The Necklace - UVa 10054 欧拉回路
2014-10-28 23:51
351 查看
The Necklace
My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace shared a common color at their meeting point. The figure below shows a segment of the necklace:But, alas! One day, the necklace was torn and the beads were all scattered over the floor. My sister did her best to recollect all the beads from the floor, but she is not sure whether she was able to collect all
of them. Now, she has come to me for help. She wants to know whether it is possible to make a necklace using all the beads she has in the same way her original necklace was made and if so in which order the bids must be put.
Please help me write a program to solve the problem.
Input
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 aline 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
2 5 1 2 2 3 3 4 4 5 5 6 5 2 1 2 2 3 4 3 1 2 4
Sample Output
Case #1 some beads may be lost Case #2 2 1 1 3 3 4 4 2 2 2
题意:是否存在使得其组成欧拉回路的方式。
思路:先判断是否为一条路径,然后确定每个点的入度和出度相同,最后输出路径。
AC代码如下:
#include<cstdio> #include<cstring> using namespace std; int T,t,n,G[60][60],d[60],p[60],vis[60]; int find(int x) { return x==p[x] ? x : p[x]=find(p[x]); } void Union(int x,int y) { x=find(x); y=find(y); p[x]=y; } bool check() { int i,ret=0; for(i=1;i<=50;i++) if(vis[i] && p[i]==i) ret++; return ret==1; } void print(int u) { int i,j,k; for(i=1;i<=50;i++) if(G[u][i]) { G[u][i]--; G[i][u]--; print(i); printf("%d %d\n",i,u); } } int main() { int i,j,k,u,v; bool flag; scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%d",&n); memset(G,0,sizeof(G)); memset(d,0,sizeof(d)); memset(vis,0,sizeof(vis)); for(i=1;i<=50;i++) p[i]=i; for(i=1;i<=n;i++) { scanf("%d%d",&u,&v); G[u][v]++; G[v][u]++; d[u]++; d[v]++; vis[u]=vis[v]=1; Union(u,v); } flag=1; if(!check()) flag=0; for(i=1;i<=50;i++) if(d[i]&1) flag=0; if(t!=1) printf("\n"); printf("Case #%d\n",t); if(flag) print(u); else printf("some beads may be lost\n"); } }
相关文章推荐
- UVA 10054 The Necklace (欧拉回路)
- UVa 10054 : The Necklace 【欧拉回路】
- UVA 10054 欧拉回路
- UVa10054 - The Necklace(欧拉回路【输出带来的麻烦)
- UVA10054-The Necklace
- uva 10054 The Necklace (欧拉回路)
- UVA 10054 The Necklace 转化成欧拉回路
- uva10054--欧拉回路--串珠子
- UVA The Necklace (并查集判断欧拉回路)
- UVA - 10054 - The Necklace (欧拉回路!!)
- uva 10054 The Necklace 欧拉回路ha
- Uva - 10054 - The Necklace(欧拉回路)
- UVA 10054 欧拉回路
- UVA 10054 The Necklace
- UVA 10054 - The Necklace 欧拉回路
- uva 10054 欧拉回路
- UVa 10054 (打印欧拉回路) The Necklace
- UVA10054 The Necklace (输出欧拉回路)
- UVA 10054 - The Necklace 欧拉回路
- UVA 10054 - The Necklace(欧拉回路)