P2341 受欢迎的牛 36分求助
  • 板块学术版
  • 楼主南瓜桐
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/3/6 20:12
  • 上次更新2023/10/28 07:06:53
查看原帖
P2341 受欢迎的牛 36分求助
439327
南瓜桐楼主2022/3/6 20:12
#include <iostream>
#include <cstdio> 
#include <vector>
using namespace std;
namespace wzl{
int m,n;
int ans=0,ds=0;
const int maxn = 2e4 + 1;
const int maxm = 5e4 + 1;
int low[maxn]={},dfn[maxn]={},co[maxn]={},col=0,st[maxn]={},top=0,num=0,dss[maxn]={};
vector<int>head,nxt,to;
void add(int u,int v){
	to.push_back(v);
	nxt.push_back(head[u]);
	head[u] = nxt.size() - 1;
	return;
}
void tarjan(int u){
	dfn[u] = low[u] = ++num;
	st[++top] = u;
	for(int i = head[u]; i != -1; i = nxt[i]){
		int v = to[i];
		if(dfn[v] == 0){
			tarjan(v);
			low[u] = min(low[u],low[v]);
		}else if(co[v] == 0){
			low[u] = min(low[u],low[v]);
		}
	}
	if(low[u] == dfn[u]){
		co[u] = ++col;
		while(st[top] != u){
			co[st[top]] = col;
			++ds;
			--top;
		}
		
		++ds;--top;
		dss[col] = ds;
		ds = 0;
	}
	return;
}
void main(){
	cin>>n>>m;
	head.resize(n+1,-1);
	for(int i = 1; i <= m; ++i){
		int u,v;
		cin>>u>>v;
		add(u,v);
	}
	
	for(int i = 1; i <= n; ++i){
		
		if(dfn[i] == 0){
			ds = 0;
			tarjan(i);
			
		}
	}
	int du[maxn] = {};
	for(int i = 1; i <=n; ++i){
		for(int j = head[i];j != -1; j = nxt[j]){
			int y = to[j];
			if(co[y] != co[i]){
				++du[co[y]];
			}
		}
	}
	int numm = 0,ans = 0;
	for(int i =1;i <= col; ++i){
		if(du[i] == 0){
			++numm;
			ans = dss[i];
		}
	}
	if(numm == 1){
		cout<<ans;
	}else{
		cout<<0;
	}
	return;
}
}


int main(){
	wzl::main();
	return 0;
}

QAQ
Orz

2022/3/6 20:12
加载中...