我太弱了,一道绿题P2341调了一个小时,求帮忙
  • 板块题目总版
  • 楼主YONIC
  • 当前回复13
  • 已保存回复13
  • 发布时间2022/3/31 18:46
  • 上次更新2023/10/28 05:04:06
查看原帖
我太弱了,一道绿题P2341调了一个小时,求帮忙
536439
YONIC楼主2022/3/31 18:46
#include<bits/stdc++.h>
#define M3 (int)(1e4+3)
#define M4 (int)(5e4+3)
using namespace std;
vector<int>G[M4];
stack<int>S;
int n,m,u,v,cnt,sccsum,scc[M3],scccnt[M3],dfn[M3],low[M3],vis[M3],out[M3];
void tarjan(int x){
	dfn[x]=low[x]=++cnt;
	S.push(x);
	for(int i=0;i<G[x].size();++i){
		int y=G[x][i];
		if(!dfn[y]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if(!scc[x]) low[x]=min(low[x],dfn[y]);
	}
	if(low[x]==dfn[x]){
		scc[x]=++sccsum;
		++scccnt[sccsum];
		while(S.top()!=x){
			++scccnt[sccsum];
			scc[S.top()]=sccsum;
			S.pop();
		}
		S.pop();
	}
}
int main(){
	scanf("%d%d",&n,&m);
	while(m--){
		scanf("%d%d",&u,&v);
		G[v].push_back(u);
	}
	for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i);
	for(int i=1;i<=n;++i) for(int j=0;j<G[i].size();++j) if(scc[i]!=scc[G[i][j]]) ++out[scc[G[i][j]]];
	int ans=0,flag=0;
	for(int i=1;i<=sccsum;++i) if(!out[i]) ans=scccnt[i],++flag;
	if(flag==1) printf("%d",ans);
	else printf("0");
	return 0;
}
2022/3/31 18:46
加载中...