一个无聊的Hack
查看原帖
一个无聊的Hack
549521
boy♂Next♂dooor楼主2024/8/21 12:28
5 4
514 666
3 5
114 514
666 114

RA:1 WA:0

题目可没说a和b都属于1到n。要额外存点

我用了个ver数组

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=5*1e4+5,N=1e4+5;
vector<int> g[M];
stack<int> s;
int n,m,cnt,tot;                                
int scc[N],siz[N],dfn[N],low[N],ver[2*M];    
void tarjan(int u)
{        
	dfn[u]=low[u]=++tot;
	s.push(u);	
	for(int v:g[u])
	{
		if(!dfn[v])  
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(!scc[v]) low[u]=min(low[u],dfn[v]);
	}	
	if(dfn[u]==low[u])//u为环顶
	{
		int x;cnt++;
		do{
			x=s.top();
			s.pop(); 
			scc[x]=cnt;
			siz[cnt]++;
		}while(x!=u);
	}														
}       	
int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	int t=0;
	for(int i=1;i<=m;i++)
	{
		int x,y;
		cin>>x>>y;
		ver[++t]=x,ver[++t]=y;
		g[x].push_back({y});
	}
	for(int i=1;i<=2*m;i++)
	{
		if(!dfn[ver[i]]) tarjan(ver[i]);
	}
	int ans=0;
	for(int i=1;i<=cnt;i++)
	{
		if(siz[i]>1) ans++;
	}
	cout<<ans;
 	return 0;
}

2024/8/21 12:28
加载中...