UVa10054 The Necklace
来源:互联网 发布:cad切剖面软件 编辑:程序博客网 时间:2024/05/20 01:35
Problem D: The Necklace
My little sister had a beautiful necklace made of colorful beads. Twosuccessive 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 scatteredover the floor. My sister did her best to recollect all the beads from thefloor, 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 knowwhether it is possible to make a necklace using all the beads she has in thesame way her original necklace was made and if so in which order the bids mustbe 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 theintegerT.
The first line of each test case contains an integer N ()giving the number of beads my sister was able to collect. Each ofthe 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 inthe sample output. Then if you apprehend that some beads may be lost justprint the sentence ``some beads may be lost" on a line by itself.Otherwise, printN lines with a single bead description on each line. Eachbead description consists of two integers giving the colors of its two ends.For,the second integer on linei must be the same as thefirst integer on line i + 1. Additionally, the second integer on lineNmust 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
Miguel Revilla
2000-12-28
这题的大意是有一串项链断了掉落在地上,然后要根据每两个节点的颜色判断是否能串成完整的项链,若能则按顺序输出各节点,否则就输出“some beads may be lost”。这题实际上就是一道判断欧拉回路的题目,用并查集先将各个节点连结并判断是否为连通图,然后再判断各个节点的度数是否为偶数,即可判定是否为欧拉图,然后输出即可。
#include<iostream>#include<cstring>using namespace std;const int MAX = 51;int T;int N;int graph[MAX][MAX];int degree[MAX];int parent[MAX];void initialize();int ancestor(int x);bool union_find_sets();bool euler();void display(int k);void initialize(){for (int i = 1; i < MAX; i++){parent[i] = i;}}int ancestor(int x){return parent[x] == x?x:parent[x] = ancestor(parent[x]);}bool union_find_sets(){int ai;int aj;for (int i = 1; i < MAX; i++){for (int j = 1; j <= i; j++){if (graph[i][j]){ai = ancestor(i);aj = ancestor(j);if (ai != aj){parent[ai] = aj;}}}}int p = 0;for (int i = 1; i < MAX; i++){if (degree[i]){p = ancestor(i);break;}}for (int i = 1; i < MAX; i++){if (degree[i]){if (p != ancestor(i))return false;}}return true;}bool euler(){for (int i = 1; i < MAX; i++){if (degree[i] && degree[i] % 2){return false;}}return true;}void display(int k){for (int l = 1; l < MAX; l++){if (graph[k][l] && degree[k] && degree[l]){degree[k]--;degree[l]--;graph[k][l]--;graph[l][k]--;display(l);cout << l << " " << k << endl;}}}int main(){cin >> T;for (int i = 1; i <= T; i++){memset(graph,0,sizeof(graph));memset(degree,0,sizeof(degree));initialize();cin >> N;int x;int y;for (int j = 1; j <= N;j++){cin >> x;cin >> y;graph[x][y]++;graph[y][x]++;degree[x]++;degree[y]++;}cout << "Case #" << i << endl;if (union_find_sets() && euler()){for (int k = 1; k < MAX; k++){if (degree[k]){display(k);break;}}}elsecout << "some beads may be lost" << endl;if (i != T)cout << endl;}return 0;}
- uva10054-The Necklace(项链)
- The Necklace UVA10054
- UVA10054 The Necklace
- UVa10054 The Necklace
- The Necklace UVA10054
- uva10054 The Necklace
- UVA10054 The Necklace
- UVA10054-The Necklace
- The Necklace UVA10054
- UVa10054 The Necklace,无向图求欧拉回路
- UVa10054 The Necklace (欧拉回路)
- 【UVa10054】The Necklace【欧拉回路】
- UVA10054 The Necklace (输出欧拉回路)
- UVA10054 The Necklace 无向图欧拉回路+连通性
- uva10054 The Necklace (欧拉回路路径输出 (并查集 + DFS) || (DFS + stack))
- The Necklace
- uva10054
- UVA10054
- kernel config
- window和frame
- 数据结构主要参考书
- 位置识别编程指南
- 表单对象
- UVa10054 The Necklace
- MySQL使用界定符导入导出
- Algorithms Part 1-Question 2-QuickSort-快速排序算法
- Android-- SharedPreferences数据存储XML文件的方法
- linux下ssh无密码登录
- 8051f CH375 优盘---ch375.c
- PHP中采用POST方式发送数据
- lucene索引合并
- 现在的你, 凭什么翘着二郎腿, 你应该放下你的腿, 身体前倾, 时刻保持战斗姿态