Unaccept100求助
查看原帖
Unaccept100求助
893608
cx2021ghj楼主2024/10/16 16:47
#include<bits/stdc++.h>
using namespace std;
int u,v,j,tot,n,m,head[10009],cnt,last;
bool vis[5009],h[5009],t;
struct ghj{
	int to,next;
}edge[10009];
void addedge(int x,int y){
	edge[++tot].next=head[x];
	edge[tot].to=y;
	head[x]=tot;
}
void dfs(int x){
	cout<<x<<" ";
	vis[x]=1;
	int c[5009],cnt=0;
	for(int i=head[x];i;i=edge[i].next){
		int v=edge[i].to;
		if(!vis[v]) c[++cnt]=v;
	}
	sort(c+1,c+1+cnt);
	for(int i=1;i<=cnt;i++){
		if(h[c[i]]==0 || h[x]==0) dfs(c[i]);
		else{
			if(i!=cnt){
				last=c[i+1];
				dfs(c[i]);
			}
			else{
				if(c[i]>last && t){
					t=0;
					return;
				}
				else dfs(c[i]);
			}
		}
	}
}
bool find_Cir(int x,int fa){
	vis[x]=1;
	for(int i=head[x];i;i=edge[i].next){
		int v=edge[i].to;
		if(vis[v] && v!=fa){
			h[x]=1;
			j=v;
			return 1;
		}
		else{
			if(v!=fa && find_Cir(v,x)){
				h[x]=1;
				if(j==x) return 0;
				else return 1;
			}
		}
	}
	return 0;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>u>>v;
		addedge(u,v);
		addedge(v,u);
	}
	j=find_Cir(1,0);
	vis[1]=t=1;
	memset(vis,0,sizeof(vis));
	dfs(1);
	return 0;
}
2024/10/16 16:47
加载中...