76pts TLE #13 #21 #22 求条(悬一关)
查看原帖
76pts TLE #13 #21 #22 求条(悬一关)
1059308
Ame_Rain_chan楼主2024/9/28 17:23

rt,感谢不尽

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e4+10,M=1e5+10;
int e[M],ne[M],h[N],idx;
struct pt{
	int a,b;
}p[N];
inline void add(int a,int b) {p[idx]={a,b};e[idx]=b;ne[idx]=h[a];h[a]=idx++;}
int dfn[N],low[N],tsp,sz[N],cnt,scc[N];
int st[N],tt=-1;
bool ist[N];
void tarjan(int u){
	dfn[u]=low[u]=++tsp;
	st[++tt]=u;ist[u]=true;
	for(int i=h[u];i != -1;i=ne[i]){
		int j=e[i];
		if(!dfn[j]){
			tarjan(j);
			low[u]=min(low[u],low[j]);
		} else if(ist[j]) low[u]=min(low[u],dfn[j]);
	}
	if(dfn[u] == low[u]){
		int t=0;
		cnt++;
		do{
			t=st[tt--];
			ist[t]=false;
			scc[t]=cnt;
			sz[cnt]++;
		} while(t != u);
	}
}
int n,m,d[N];
set<int> v[N];
int main() {
	memset(h,-1,sizeof(h));
	scanf("%d%d",&n,&m);
	while(m--){
		int a,b;scanf("%d%d",&a,&b);
		add(a,b);
	}
	for(int i=1;i <= n;i++){
		if(!dfn[i]) tarjan(i);
	}
	for(int i=0;i<idx;i++){
		int a=scc[p[i].a],b=scc[p[i].b];
		if(a != b && (v[a].empty() || *v[a].lower_bound(b) != b)){
			v[a].insert(b);
			d[a]++;
		}
	}
	int num=0,ans=0;
	for(int i=1;i <= cnt;i++){
		for(int i=1;i <= cnt;i++){
			if(!num && !d[i]) ans=sz[i],num=ans;
			else if(num && !d[i]) {printf("0\n");return 0;}
		}
	}
	printf("%d\n",ans);
	return 0;
}
2024/9/28 17:23
加载中...