题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878
3 3 1 2 1 3 2 3 3 2 1 2 2 3 0
1 0
无向欧拉图的欧拉回路存在的充要条件:连通且没有奇点;
欧拉路径存在的充要条件:连通且奇点个数为2;
有向欧拉图的欧拉回路存在的充要条件:基图连通且所有顶点的入度等于出度;
欧拉路径存在的充要条件:基图连通且存在某顶点入度比出度多一,
另一顶点出度比入度多一,其余顶点入度等于出度。
代码如下:
#include <cstdio>
#include <cstring>
int father[1017];
int n, m;
int find(int x)
{
return x==father[x]?x:father[x]=find(father[x]);
}
void init()
{
for(int i = 0; i <= n; i++)
{
father[i] = i;
}
}
void Union(int x, int y)
{
int f1 = find(x);
int f2 = find(y);
if(f1 != f2)
father[f2] = f1;
}
int main()
{
int i, j;
int a, b;
int cal[1017];
while(scanf("%d",&n)&& n)
{
init();
memset(cal,0,sizeof(cal));
if(n == 0)
break;
scanf("%d",&m);
for(i = 0; i < m; i++)
{
scanf("%d%d",&a,&b);
Union(a,b);
cal[a]++,cal[b]++;
}
int flag = 0;
int t = father[1];
for(i = 1; i <= n; i++)
{
if(cal[i]%2 == 1 || father[i] != t)
{
flag = 1;
break;
}
}
if(flag)
printf("0\n");
else
printf("1\n");
}
return 0;
}
hdu1878 欧拉回路(并查集),布布扣,bubuko.com
原文:http://blog.csdn.net/u012860063/article/details/37913257