求助,第18,19个点过不去
查看原帖
求助,第18,19个点过不去
78390
沧海映繁星楼主2021/9/10 09:30
#include<bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i = l; i <= r; ++i)
#define dep(i,l,r) for(int i = r; i >= l; --i)
#define next(i,u) for(int i = head[u]; i; i = a[i].next)
#define N 2000005
int read(){
	int x = 0,f = 1;
	char s = getchar();
	while(s < '0' || s > '9'){
		if(s == '-') f = -1;
		s = getchar();
	}
	while(s >= '0' && s <= '9'){
		x = (x << 1) + (x << 3) + s - '0';
		s = getchar();
	}
	return x * f;
}
struct road{
	int l,r;
}t[N];
struct node{
	int next,to;
}a[N];
int n,m,u,v,tot,pos,num,cnt,head[N],c[N];
int dfn[N],low[N],f[N],s[N],g[N];
bool vis[N];
queue<int> p;
stack<int> st;
bool cmp(road x,road y){
	if(x.l < y.l) return 1;
	if(x.l == y.l && x.r < y.r) return 1;
	return 0;
}
void add(int u,int v){
	a[++tot].next = head[u];a[tot].to = v;head[u] = tot;
}
void tarjan(int u){
	vis[u] = 1;
	dfn[u] = low[u] = ++cnt;
	st.push(u);
	next(i,u){
		int v = a[i].to;
		if(!dfn[v]){
			tarjan(v);
			low[u] = min(low[u],low[v]);
		}
		else if(vis[v]) 
		low[u] = min(low[u],dfn[v]);
	}
	if(dfn[u] == low[u]){
		int x;pos++;
		while(1){
			x = st.top();st.pop();
			vis[x] = 0;
			f[x] = pos;
			c[pos]++;
			if(x == u) break;
		}
	}
}
void work(){
	while(!p.empty()){
		int u = p.front();
		p.pop();
		next(i,u){
			int v = a[i].to;
			s[v]--;g[v] += g[u];
			if(!s[v]) p.push(v);
		}
	}
}
signed main(){
	n = read(); m = read(); pos = n;
	rep(i,1,m) u = read(),v = read(),add(u,v); 
	rep(i,1,n) if(!dfn[i]) tarjan(i);
	rep(u,1,n) next(i,u){
		int v = a[i].to;
		if(f[u] == f[v]) continue;
		t[++num].l = f[u];t[num].r = f[v]; 
	}
	sort(t + 1,t + num + 1,cmp);
	rep(i,1,num){
		if(t[i].l == t[i - 1].l && t[i].r == t[i - 1].r) continue;
		add(t[i].l,t[i].r); s[t[i].r]++;
	}
	rep(i,n + 1,pos) g[i] = c[i];
	rep(i,n + 1,pos) if(!s[i]) p.push(i);
	work(); rep(i,n + 1,pos)
	if(g[i] == n){printf("%d",c[i]);return 0;}
	puts("0");
}
2021/9/10 09:30
加载中...