UVA 10054 The Necklace 欧拉回路
来源:互联网 发布:深圳奇迹智慧网络 编辑:程序博客网 时间:2024/05/04 01:01
The input contains T test cases. The first line of the input contains the integer T.
The first line of each test case contains an integer N ( ) giving the number of beads my sister was able to collect. Each of the next N lines contains two integers describing the colors of a bead. Colors are represented by integers ranging from 1 to 50.
Output
For each test case in the input first output the test case number as shown in the sample output. Then if you apprehend that some beads may be lost just print the sentence ``some beads may be lost" on a line by itself. Otherwise, print N lines with a single bead description on each line. Each bead description consists of two integers giving the colors of its two ends. For , the second integer on line i must be the same as the first integer on line i + 1. Additionally, the second integer on line N must be equal to the first integer on line 1. Since there are many solutions, any one of them is acceptable.
Print a blank line between two successive test cases.
Sample Input
251 22 33 44 55 652 12 23 43 12 4
Sample Output
Case #1some beads may be lost Case #22 11 33 44 22 2
判断欧拉路是否存在的方法
有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。
无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的。
判断欧拉回路是否存在的方法
有向图:图连通,所有的顶点出度=入度。
无向图:图连通,所有顶点都是偶数度。
对于此题无向图的欧拉回路来说就是判断所有的点都是偶数度,但是这还不够,要用并查集判断一下连通性。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>using namespace std;const int maxn=55;int d[maxn],mp[maxn][maxn];int pre[maxn];int n;void init(){ memset(mp,0,sizeof(mp)); memset(d,0,sizeof(d)); for(int i=0;i<maxn;i++) pre[i]=i;}int find_root(int x){ if(x!=pre[x]) pre[x]=find_root(pre[x]); return pre[x];}bool ok(){ int x=-1; for(int i=0;i<maxn;i++) { if(d[i]&1) return false;; if(d[i]) { if(x==-1) x=find_root(i); else { if(x!=find_root(i)) return false; } } } return true;}void dfs(int x)//dfs搜索下路径输出{ for(int i=1;i<maxn;i++) { if(mp[x][i]) { mp[x][i]--;//特别注意可能不止多条 mp[i][x]--; dfs(i); printf("%d %d\n",i,x); } }}int main(){ int x,y; int l=0; int ss,t; scanf("%d",&t); while(t--) { scanf("%d",&n); init(); for(int i=0;i<n;i++) { scanf("%d%d",&x,&y); ss=x; d[x]++; d[y]++; mp[x][y]++; mp[y][x]++; pre[find_root(x)]=find_root(y); } if(l) printf("\n"); printf("Case #%d\n",++l); if(!ok()) printf("some beads may be lost\n"); else dfs(ss); } return 0;}
- UVa 10054 - The Necklace(欧拉回路)
- UVA 10054 the necklace 欧拉回路
- UVA 10054 - The Necklace 欧拉回路
- UVa 10054 - The Necklace 欧拉回路
- uva 10054 The Necklace (欧拉回路)
- UVa 10054 The Necklace(欧拉回路)
- UVA 10054 The Necklace 欧拉回路
- The Necklace - UVa 10054 欧拉回路
- UVA 10054 - The Necklace(欧拉回路)
- uva 10054 The Necklace 欧拉回路
- uva 10054 The Necklace 欧拉回路
- UVA 10054 The Necklace(欧拉回路)
- UVA - 10054 The Necklace 欧拉回路
- UVa 10054 - The Necklace, 欧拉回路+打印路径
- uva 10054 The Necklace(欧拉回路)
- Uva - 10054 - The Necklace(欧拉回路)
- UVA 10054 The Necklace(欧拉回路+输出路径)
- UVA - 10054 The Necklace (欧拉回路)
- thrift入门实例
- C语言作用域、存储类型、链接类型、static关键字
- ie8以下版本(包括ie8)的浏览器不支持html5标签属性解决方案(Modernizr 2.6.2插件的使用)
- 机房收费系统之SQL数据类型
- vmware下网络配置
- UVA 10054 The Necklace 欧拉回路
- wsdl_cache_enabled = 0
- c/c++程序之_KMP字符串模式匹配详解(非常不错的详解)
- 八大排序算法的实现
- uva 1561
- 黑马程序员——Html常用标签速查以及CSS按知识点速查手册
- Eclipse加入extjs卡的解决方案
- 苹果开发者账号申请
- i2c裸机-汇编图解(51)