求助UKE
查看原帖
求助UKE
285617
黑影洞人楼主2022/4/1 17:26
#include<cstdio>
#include<algorithm>
#define N 1919810
using namespace std;
int cc,n,m,head[N],to[N],nxt[N],tot,dfn[N],low[N],col,co[N],st[N],top,cnt;
void add(int u,int v){
	to[++tot]=v;
	nxt[tot]=head[u];
	head[u]=tot;
}
void tarjan(int x){
	st[++top]=x;
	dfn[x]=low[x]=++cnt;
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		if(!dfn[y])tarjan(y),low[x]=min(low[x],low[y]);
		else if(!co[y])low[x]=min(low[x],dfn[y]);
	} 
	if(dfn[x]==low[x]){
		co[x]=++col;
		int siz=1;
		while(st[top]!=x){
			co[st[top--]]=col;
			siz++;
		}top--;
		if(siz>=2)cc++;
	}
}
signed main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		add(a,b),add(b,a);
	}
	for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
	if(cc>=2)puts("NO");
	else puts("YES");
	return 0;
}



2022/4/1 17:26
加载中...