UVA The Necklace (并查集判断欧拉回路)
2015-01-22 16:57
351 查看
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" on a line by itself. Otherwise, print N lineswith 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
Miguel Revilla
2000-12-28
题意:有一个项链其中的每一个珠子都有两种颜色,每个珠子相连的规则是其中的珠子的两种颜色都能匹配到和他颜色一样的珠子,如题目所给的图所示。现在这样的一串项链的珠子散落了,只找到了其中的一部分,问是否可以用找到的这一部分珠子重新做一个和原来项链规则一样的。能的话输出新项链的组成顺序,(输出不唯一),不能输出some .....;
并查集判断欧拉回路。
代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> const int M = 100; int num[M]; int map[M][M]; int n; int findx(int x) { int r = x; while(r!=num[r]) { r = num[r]; } int k = x,j; while(k!=r) { j = num[k]; num[k] = r; k = j; } return r; } void bing(int x,int y) { int fx = findx(x); int fy = findx(y); if(fx!=fy) { num[fx] = fy; } } void print(int k) { for (int i = 0; i < M; i++) { if (map[k][i]) { map[k][i]--; map[i][k]--; print(i); printf("%d %d\n", i,k); } } } int main() { int t, k = 1; int f[M]; scanf("%d" ,&t); while (t--) { memset(f, 0, sizeof(f)); memset(map, 0, sizeof(map)); int sum = 0; for (int i = 0; i < M; i++) { num[i] = i; } scanf("%d", &n); for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); f[a]++; f[b]++; map[a][b]++; map[b][a]++; bing(a,b); } int begin = 0; for (int i = 0; i < M; i++) { if (f[i] && findx(i) == i) { begin = i; break; } } for (int i = 0; i < M; i++) { //printf("num[%d] = %d\n",i,num[i]); sum += f[i] % 2; if (f[i] && begin != findx(i)) { sum++; } } printf("Case #%d\n", k++); if (sum > 0) { printf("some beads may be lost\n"); } else { // printf("begin = %d\n",begin); print(begin); } if (t!=0) { printf("\n"); } } return 0; }
相关文章推荐
- UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)
- poj Colored Sticks(字典树 欧拉回路的判断 并查集)
- UVA 10129 欧拉回路 判断连通性
- UVA - 10596 - Morning Walk(并查集判断连通图)
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
- UVA 10054 The Necklace (欧拉回路)
- poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断
- 计蒜客 判定欧拉回路 (利用并查集实现有向图欧拉回路的判断)
- uva10596 Morning Walk (无向图,欧拉回路,存在性判断)
- UVa 12118 - Inspector's Dilemma(并查集,欧拉回路)
- CodeForces 209C Trails and Glades(欧拉回路判断+并查集计算联通分量)
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
- Uva 10129 (dfs判断连通 +欧拉回路)
- NYOJ+一笔画问题+先要判断无向图的连通性(DFS or并查集),然后使用欧拉回路。
- UVa302 - John's trip(并查集、欧拉回路、DFS)
- [poj2513]Colored Sticks Trie +并查集 + 欧拉回路判断
- (UVa 10054)The Necklace --欧拉回路的判断和输出,DFS
- The Necklace - UVa 10054 欧拉回路
- POJ1386 Play on Words 欧拉回路[并查集判断连通]
- uva10054 The Necklace (欧拉回路路径输出 (并查集 + DFS) || (DFS + stack))