您的位置:首页 > 其它

杭电hdu 1878 欧拉回路 无向图

2012-05-08 19:54 295 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1878

无向图的欧拉回路。

判断一个无向图是否存在欧拉回路,只需判断这个无向图连通并且每个顶点的度为偶数。

#include <stdio.h>
#include <string.h>

int degree[1001];
int father[1001];
int map[1001][1001];
int n;

void init()
{
int i;
memset(map, 0, sizeof(map));
for(i = 0; i <= n; i ++){
father[i] = i;
degree[i] = 0;
}
}

int getfather(int x)
{
while(x != father[x]){
x = father[x];
}
return x;
}

void join(int x, int y)
{
int fa = getfather(x);
int fb = getfather(y);
if(fa != fb)father[fb] = fa;
}

int main()
{
int m, i, from, to;
int cir;
while(scanf("%d", &n)&&n){
init();
scanf("%d", &m);
while(m --){
scanf("%d%d", &from, &to);
if(map[from][to] == 0){
map[from][to] = map[to][from] = 1;
degree[from] ++;
degree[to] ++;
join(from, to);
}
}
cir = 0;
for(i = 1; i <= n; i ++){
if(father[i] == i){
cir ++;
}
}
if(cir != 1)printf("0\n");
else {
for(i = 1; i <= n; i ++){
if(degree[i] % 2 != 0)break;
}
if(i > n){
printf("1\n");
}
else printf("0\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: