HDU 1878 欧拉回路
欧拉回路
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8309 Accepted Submission(s): 2980
束。
思路:简单欧拉回路,不需要输出欧拉回路,题目讲述的是无方向的图,只需要根据存在欧拉回路
的条件判断即可
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
int n,m;
int flag;
int a,b;
int hash[1010],map[1010];
void Merge(int a,int b)
{
int i = min(map[a],map[b]);
int j = max(map[a],map[b]);
for(int k = 1;k <= n;k ++)
if(map[k] == j)
map[k] = i;
}
int main()
{
while(scanf("%d%d",&n,&m),n != 0 && m != 0)
{
flag = 1;
memset(hash,0,sizeof(hash));
for(int i = 1;i <= n;i ++)
map[i] = i;
while(m --)
{
scanf("%d%d",&a,&b);
Merge(a,b);
hash[a] ++;hash[b] ++;
}
for(int i = 1;i <= n;i ++)
if(map[i] != 1 || hash[i] % 2 == 1 || hash[i] == 0)
{
flag = 0;
break;
}
if(flag == 1)
printf("1\n");
else
printf("0\n");
}
return 0;
}