题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878
题目的意思很简单,就是给你一个无向图。。问存在欧拉回路吗?Yes or No。1000个节点。。。
看欧拉回路的定义。。
通过图中所有边一次且仅一次行遍所有顶点是回路,称为欧拉回路。。
具有欧拉回路的图称为欧拉图。。
解这个问题,非常的简单,就需要一个定理就好了。
无向图是欧拉图当且仅当图是连通图且没有奇度顶点。。。
那么,我们就仅仅需要判断这个图的连通性和顶点的度数就好啦。。。
Code:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1e3 + 5;
int n, m, d[N], father[N];
void Init()
{
for(int i = 1; i <= n; i ++){
father[i] = i; d[i] = 0;
}
}
int find(int x)
{
if(x == father[x]) return x;
return father[x] = find(father[x]);
}
void Union(int x, int y)
{
int a = find(x), b= find(y);
if(a != b) father[x] = b;
}
bool solve()
{
int cnt = 0;
for(int i = 1; i <= n; i ++){
if(father[i] == i) cnt ++;
}
if(cnt != 1) return false;
for(int i = 1; i <= n; i++){
if(d[i] % 2) return false;
}
return true;
}
int main()
{
// freopen("1.txt", "r", stdin);
while(scanf("%d", &n) && n){
scanf("%d", &m);
Init();
int x, y;
for(int i = 0; i < m; i ++){
scanf("%d %d", &x, &y);
Union(x, y);
d[x] ++; d[y] ++;
}
if(solve()) puts("1");
else puts("0");
}
return 0;
}
慢慢的来弥补图论上的缺陷吧。。come on!! 加油。。