hdu1878 欧拉回路(并查集)
来源:互联网 发布:socket.io java服务端 编辑:程序博客网 时间:2024/06/09 17:20
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878
Problem Description
欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结
束。
束。
Output
每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
Sample Input
3 31 21 32 33 21 22 30
Sample Output
10
无向欧拉图的欧拉回路存在的充要条件:连通且没有奇点;
欧拉路径存在的充要条件:连通且奇点个数为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");elseprintf("1\n");}return 0;}
7 0
- hdu1878欧拉回路【并查集】
- hdu1878 欧拉回路(并查集)
- HDU1878 欧拉回路(并查集)
- hdu1878 欧拉回路(并查集)
- HDU1878--欧拉回路(并查集+欧拉回路)
- hdu1878欧拉回路 并查集学习 欧拉路学习
- hdu1878 欧拉回路(无向图+并查集)
- 欧拉回路 hdu1878
- hdu1878欧拉回路
- hdu1878 欧拉回路
- 欧拉回路((hdu1878))
- HDU1878欧拉回路
- 欧拉回路 hdu1878
- HDU1878欧拉回路
- hdu1878 欧拉回路
- hdu1878 欧拉回路
- HDU1878 欧拉回路
- hdu1878 欧拉回路
- RAR解密跑字典
- POJ 1068 Parencodings
- poj 1789 Truck History(kruskal算法)
- rwsem文件
- httpmodule通过httpfilter获取返回的网页内容
- hdu1878 欧拉回路(并查集)
- android 模拟按键
- android activity task 进程,线程综述
- linux echo 怎样输出转义字符?
- POJ3620 Avoid The Lakes
- 做事效率太低?你必须改掉这7种坏习惯
- C# 枚举进程模块
- java中PropertyDescriptor的使用简介
- C++ - "No source available for "__chkstk_ms() at 0x401f0b"" 异常