欧拉回路
来源:互联网 发布:relieff算法 matlab 编辑:程序博客网 时间:2024/05/22 10:53
Description
欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结
束。
Output
每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
Sample Input
3 3
1 2
1 3
2 3
3 2
1 2
2 3
0
Sample Output
1
0
Hint
题意
题解:
AC代码
#include<cstdio>#include<cstring>#include<stack>#include <set>#include <queue>#include <vector>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int mod = 1e9+7;int par[1100000];int p[1100000];int n,k;void init(int n){ for (int i = 1;i <= n; ++i){ par[i] = i; p[i] = 0; }}int fd(int x){ if (x==par[x]) return par[x]; return par[x]=fd(par[x]);}void ut(int x,int y){ int tx = fd(x); int ty = fd(y); if (tx!=ty){ par[tx] = ty; }}int main(){ while (scanf("%d",&n),n){ scanf("%d",&k); init(n); int x,y; for (int i = 1;i <= k; ++i){ scanf("%d%d",&x,&y); if (x!=y) { ut(x,y); p[x]++; p[y]++; } } int flag = 0; int tt = 1; for (int i = 1; i <= n; ++i){ if (par[i]==i) flag++; if (p[i]&1) tt = 0; } if (tt==0||flag>1){ printf("0\n"); }else printf("1\n"); } return 0;}
阅读全文
0 0
- HDU1878 欧拉回路【欧拉回路】
- 欧拉回路判断
- 欧拉回路 hdu1878
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路,欧拉路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 图论 欧拉回路
- zju2008欧拉回路
- 欧拉回路
- hdu1878欧拉回路
- Nodejs绑定域名与Nginx反向代理
- ZOJ 3210A Stack or A Queue?
- Http协议访问网络
- C++字符串和string类
- 将unicode编码转成中文
- 欧拉回路
- MySQL用户管理和访问权限
- UI 一一 资源存放及内存优化问题
- C#之规则
- 论文笔记索引
- spring配置事务管理
- sqlite学习
- pat乙级1030这个思想真的不错 比我直接暴力两层循环好多
- 前端在html页面之间传递参数的方法