题解 [JSOI2015]最小表示

link

Solution

md,智障了。

首先看出一个结论,就是说一条边 \((u,v)\) 可以删当且仅当在原图中删掉改变该边 \(u\) 仍能到达 \(v\)。

因为这是一个 DAG,所以我们可以拓排之后从后往前扫,然后用 bitset 维护连通性。

Code

#include <bits/stdc++.h>
using namespace std;
 
#define Int register int
#define MAXM 100005
#define MAXN 30005
 
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}

vector <int> g[MAXN],h[MAXN],sta[MAXN];
int n,m,px[MAXM],py[MAXM],dep[MAXN],deg[MAXN];

void topo (){
	queue <int> q;
	for (Int i = 1;i <= n;++ i){
		deg[i] = h[i].size();
		if (!deg[i]) dep[i] = 1,q.push (i);
	}
	while (!q.empty()){
		int u = q.front();
		q.pop (),sta[dep[u]].push_back (u);
		for (Int v : g[u]) if (!-- deg[v]) dep[v] = dep[u] + 1,q.push (v);
	}
}

bitset <MAXN> S[MAXN];

signed main(){
	read (n,m);
	for (Int i = 1;i <= m;++ i) read (px[i],py[i]),g[px[i]].push_back (py[i]),h[py[i]].push_back (px[i]);
	topo ();int ans = 0;
	for (Int i = n;i >= 1;-- i)
		for (Int u : sta[i]){
			sort (g[u].begin(),g[u].end(),[](int x,int y){return dep[x] < dep[y];});
			S[u][u] = 1;
			for (Int v : g[u])
				if (S[u][v]) ans ++;
				else S[u] |= S[v];
		}
	write (ans),putchar ('\n');
	return 0;
}
上一篇:缺省源


下一篇:「BZOJ2131」免费的馅饼 (DP+树状数组) 总算做出来了