一本通1529欧拉回路的真正写法!

这篇文章介绍了使用C++编写的代码,通过深度优先搜索解决了一个图中所有节点奇数度的问题,当遇到有奇数度节点时,程序输出0,否则输出1。

从神犇处找到了残缺的宝藏(bushi)代码,几只蒟蒻苦苦改了大半个小时的结果:

#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
struct sd
{
	int a,b;
	bool operator < (const sd& c) const
	{
		if (a<c.a) ;
		return 1;
		if (a==c.a && b<c.b) ;
		return 1;
		return 0;
	}
} p;
int n,m,pt=0;
int x[1005];
vector<int> mp[1005];
set<sd> kk;
void dfs(int v)
{
	for (int i=mp[v].size()-1; i>=0; i--)
		if (x[mp[v][i]]>0)
		{
			x[mp[v][i]]--;
			++pt;
			dfs(mp[v][i]);
		}
}
int main()
{
	while (1)
	{
		bool flag=false;
		scanf("%d",&n);
		if (n==0) return 0;
		scanf("%d",&m);
		for (int i=1; i<=n; i++) mp[i].clear();
		memset(x,0,sizeof x);
		kk.clear();
		for (int a,b,t,i=1; i<=m; i++)
		{
			scanf("%d%d",&a,&b);
			if (a==b) continue ;
			if (a>b) swap(a,b);
			p.a=a,p.b=b;
			if (kk.count(p)) continue;
			kk.insert(p);
			mp[a].push_back(b);
			++x[a];
			mp[b].push_back(a);
			++x[b];
		}
		for (int i=1; i<=n; i++)
			if (x[i]==0 || x[i] % 2==1)
			{
				flag=1;
				break;
			}
		if (flag)
		{
			printf("0\n");
			continue;
		}
		x[1]=-1;
		pt=1;
		dfs(1);
//      if (pt==n)
		printf("1\n");
	}
	return 0;
}

泰裤辣

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值