tarjan缩点板子题48pts满江WA求助!
查看原帖
tarjan缩点板子题48pts满江WA求助!
682028
_awa_keyai楼主2023/7/28 20:31

评测记录:https://www.luogu.com.cn/record/117891271

//呆萌可爱的代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+55;
struct edge{
	int to;
	edge(int to_){
		to=to_;
	}
};
vector<edge> g[maxn];
int dfn[maxn],siz[maxn],low[maxn],scc[maxn],ins[maxn],cnt,sccn,ond[maxn];
int N,M;
stack<int> s;

void tarjan(int u){
	dfn[u]=low[u]=++cnt;
	s.push(u);
	ins[u]=1;
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i].to;
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		} else if(ins[v]){
			low[u]=min(low[u],dfn[v]);
		}
	}
	
	if(low[u]==dfn[u]){
		ins[u]=0;
		scc[u]=++sccn;
		siz[sccn]=1;
		while(s.top()!=u){
			scc[s.top()]=sccn;
			ins[s.top()]=0;
			siz[sccn]+=1;
			s.pop();
		}
		s.pop();
	}
	return;
}

signed main(void){
	
	cin>>N>>M;
	memset(low,0x3f,sizeof(low));
	for(int i=1;i<=M;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(edge(v));
	}
	
	for(int i=1;i<=N;i++){
		if(!dfn[i]){
			cnt=0;
			tarjan(i);
		}
	}
	
	for(int i=1;i<=N;i++){
		for(int j=0;j<g[i].size();j++){
			int v=g[i][j].to;
			if(scc[i]!=scc[j]) ond[scc[i]]++;
		}
	}
	
	int cont=0,ans=0;
	for(int i=1;i<=sccn;i++){
		if(ond[i]==0){
			cont++;
			ans+=siz[i];
		}
	}
	
	if(cont==1){
		cout<<ans<<endl;
	} else {
		cout<<0<<endl;
	}
	
	return 0;
}
2023/7/28 20:31
加载中...