紧急求救18pts:图论——RE+最后一点WA
查看原帖
紧急求救18pts:图论——RE+最后一点WA
1050431
DevilsFlame楼主2024/8/2 10:04
#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,x,y,s[1000010],top;
vector <int> q[1000010];
bool p[10000010];
vector <vector <int> > ans;//答案
bool dfs(int v,int fa)
{
	if(p[v])
	{
		int x;
		vector <int> e;
		e.push_back(v);
		x = s[top];
		while(s[top] != v)//去栈
		{
			e.push_back(s[top --]);
			p[v] = 0;
			x = s[top];
		}
		e.push_back(v);
		ans.push_back(e);
		return 1;
	}
	s[++ top] = v;
	p[v] = 1;
	bool e = 0;
	bool l = 1;
	for(int i = 0;i < q[v].size();)
	{
		int u = q[v][i];
		l = 0;
		q[v].erase(q[v].begin() + i);//删边操作
		if(u == fa) continue;
		if(dfs(u,v)) e = 1;
	}
	p[v] = 0;
	if(l) return 1;
	return e;
}
int main()
{
	ios::sync_with_stdio(0);
	cin >> n >> m;
	for(int i = 1;i <= m;i ++)
	{
		cin >> a >> b >> x >> y;
		if(x != y)
		{
			q[a].push_back(b);
			q[b].push_back(a);
		}
	}
	for(int i = 1;i <= n;i ++)
	{
		if(!dfs(i,0))
		{
			cout << "NIE\n";
			return 0;
		}
	}
	int r = ans.size();
	cout << r << endl;
	for(int i = 0;i < r;i ++)
	{
		int l = ans[i].size();
		cout << l - 1 << ' ';
		for(int j = 0;j < l;j ++)
		    cout << ans[i][j] << ' ';
		cout << endl;
	}
	return 0;
}
2024/8/2 10:04
加载中...