The Necklace+uva+求欧拉回路并输出路径
2014-08-05 12:52
363 查看
The Necklace
Description
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.
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.
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.
解决方案:想判断该图能否构成欧拉回路,无向图的存在欧拉回路的充分必要条件是每个节点的度都为偶数。然后在深度优先搜索求出路径。
code:
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %ll |
Problem D: 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" ona 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
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
解决方案:想判断该图能否构成欧拉回路,无向图的存在欧拉回路的充分必要条件是每个节点的度都为偶数。然后在深度优先搜索求出路径。
code:
#include<iostream> #include<cstdio> #include<cstring> #define MMAX 1003 using namespace std; int N; int Map[55][55]; bool vis[55]; int d[55]; struct node { int a,b; void pu(int aa,int bb) { a=aa,b=bb; } } save[MMAX]; bool flag; void dfs(int st) { for(int i=1; i<=50; i++) { if(Map[st][i]) { Map[i][st]--,Map[st][i]--; dfs(i); printf("%d %d\n",i,st); } } } int main() { int t,k=0; scanf("%d",&t); while(t--) { scanf("%d",&N); memset(Map,0,sizeof(Map)); memset(vis,false,sizeof(vis)); memset(d,0,sizeof(d)); int a,b; for(int i=0; i<N; i++) { scanf("%d%d",&a,&b); Map[b][a]++,Map[a][b]++; d[a]++,d[b]++; vis[a]=vis[b]=true; } int cnt=0; for(int i=1; i<=50; i++) { if(vis[i]&&d[i]%2==1) cnt++; } printf("Case #%d\n",++k); if(!cnt) { dfs(0,a); for(int i=0;i<N;i++){ printf("%d %d\n",save[i].a,save[i].b); } } else { printf("some beads may be lost\n"); } if(t) cout<<endl; } return 0; }
相关文章推荐
- The Necklace UVA - 10054 题解(欧拉回路,路径输出)
- uva10054 The Necklace (欧拉回路路径输出 (并查集 + DFS) || (DFS + stack))
- UVA 10054 The Necklace(欧拉回路+输出路径)
- (UVa 10054)The Necklace --欧拉回路的判断和输出,DFS
- UVa10054 - The Necklace(欧拉回路【输出带来的麻烦)
- UVA10054 The Necklace (输出欧拉回路)
- uva_10054_The Necklace(欧拉回路+打印路径)
- [UVA 10054]The Necklace[欧拉回路][打印路径]
- UVA 10054 The Necklace(欧拉回路,打印路径)
- poj 1637判定混合图欧拉回路 uva10735 混合欧拉回路+路径输出
- UVA 10054 - The Necklace 欧拉回路
- UVa10054 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 欧拉回路
- UVA10054 The Necklace(欧拉回路)