点击打开链接hdu 1878
思路: 欧拉回路
分析:
1 首先题目给定的是一个无向图,所以我们判断该图是欧拉图的条件是图是否是连通的并且所有点的度都是偶数
2 很明显度数很好判断,而图是否连通通过并查集
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 1010;
int father[MAXN] , degree[MAXN];
int n , m;
void init(){
memset(degree , 0 , sizeof(degree));
for(int i = 1 ; i <= n ; i++)
father[i] = i;
}
int find(int x){
if(x != father[x])
father[x] = find(father[x]);
return father[x];
}
bool isOk(){
int root = -1;
for(int i = 1 ; i <= n ; i++){
if(degree[i]&1)
return false;
if(degree[i]){
if(root == -1)
root = find(i);
else{
if(root != find(i))
return false;
}
}
}
return true;
}
int main(){
while(scanf("%d" , &n) && n){
scanf("%d" , &m);
init();
int x , y;
for(int i = 0 ; i < m ; i++){
scanf("%d%d" , &x , &y);
degree[x]++;
degree[y]++;
father[find(x)] = find(y);
}
if(!isOk())
puts("0");
else
puts("1");
}
return 0;
}
分享到:
相关推荐
算法-欧拉回路(HDU-1878)(包含源程序).rar
hdu 1695 GCD(欧拉函数+容斥原理).docx
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
HDU的一题........HDU DP动态规
hdu 1574 passed sorce
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
HDU最全ac代码
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
hdu动态规划算法集锦
hdu 1166线段树代码
hdu题目分类
HDU图论题目分类