为什么删去dfs的第二个return就错了
查看原帖
为什么删去dfs的第二个return就错了
653210
LJCzzzzZ楼主2022/8/4 17:43
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e6+10, M = 1e5+10;
struct node{
	int to,ne;
}edge[N];
int n, m;
int d[M],st[N],f[N];
int head[N],cnt;
int ans,tot;
void add(int a,int b){
	edge[cnt].to = b;
	edge[cnt].ne = head[a];
	head[a] = cnt++;
}

void dfs(int u,int root)//dfs找环 
{
	f[++tot]=u;//入栈 
	d[u]-=2;//出来进去各一次 
	for(int i=head[u];i!=-1;i=edge[i].ne)
	{
		//head[u]=i;
		int j = edge[i].to;
		if(st[i]) continue;
		st[i]=st[i^1]=1;//走过的边就不走了 
		if(u!=root&&j==root)//此时开头结尾已重合,这个环完了 
		{
			ans++;//计算总环数 
			f[++tot]=j;//入栈 
			return;
		}
		dfs(j,root);
		return;
	}
}
int main() 
{
	memset(head,-1,sizeof head);
	scanf("%d%d",&n,&m);
	for(int i = 1; i <= m; i++ ){
		int x,y,z,w;
		scanf("%d%d%d%d",&x,&y,&z,&w);
		if(z^w){
			add(x,y);
			add(y,x);
			d[x]++;
			d[y]++;
		}
	}
	for(int i = 1; i <= n; i++ ){
		if(d[i]%2){
			printf("NIE\n");
			return 0;
		}
	}
	for(int i = 1; i <= n; i++ ){
		if(!d[i]) continue;
		while(d[i]) dfs(i,i);
	}
	printf("%d\n",ans);
	int i = 1;
	while(i<=tot){
		int root = f[i], k = 1;
		i++;
		while(f[i]!=root&&i<=tot) i++,k++;
		printf("%d ",k);
		for(int j = i-k; j <= i; j++ ) printf("%d ",f[j]);
		printf("\n");
		i++;
	}
	return 0;
}
2022/8/4 17:43
加载中...