uva 10054 The Necklace(欧拉回路)

来源:互联网 发布:tcl智能电视直播软件 编辑:程序博客网 时间:2024/06/07 07:46

                       uva 10054 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 integerT.

The first line of each test case contains an integer N ( $5 \leN \le 1000$) 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, printN lines with a single bead description on each line. Each bead description consists of two integers giving the colors of its two ends. For$1 \le i \le N ­ 1$, the second integer on linei must be the same as the first integer on line i + 1. Additionally, the second integer on lineN 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、所有点的入度要等于出度;

2、所有点的联通

输出的时候要注意点的自身形成一个环

比如:

1 -> 2

2  -> 3

3 -> 1

2 -> 4

4 -> 2

欧拉回路需要逆序输出。

#include<stdio.h>#include<string.h>int gra[550][550], num[550], vis[550], n;int get(int x) {  return num[x] != x ? get(num[x]) : x;}void print(int a){ //逆序输出 for (int i = 0; i < 55; i++)  if (gra[a][i]){  gra[a][i]--;  gra[i][a]--;  print(i);  printf("%d %d\n", i , a);  }  }  int main() {int T, cnt = 1;scanf("%d", &T);while (T--) {memset(vis, 0, sizeof(vis));memset(gra, 0, sizeof(gra));scanf("%d", &n);int a, b;for (int i = 0; i < 55; i++) {num[i] = i;}for (int i = 0; i < n; i++) {scanf("%d %d", &a, &b);vis[a]++;vis[b]++; //统计 度gra[a][b]++;gra[b][a]++; //构建图num[get(a)] = get(b); //联通}int begin = 0;for (int i = 0; i < 55; i++) {if (vis[i] && get(i) == i) { //若度不为零,且为环的起始点begin = i;break;}}int flag = 0;for (int i = 0; i < 55; i++) {flag += vis[i] % 2; if (vis[i] && begin != get(i)) { //不连通flag++;}}printf("Case #%d\n", cnt++);if (flag) printf("some beads may be lost\n");else print(begin);if (T) printf("\n");}return 0;}



0 0
原创粉丝点击