Problem D: The Necklace
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 theinteger T.
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, print N 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 line i must be the same as thefirst integer on line i + 1. Additionally, the second integer on line Nmust 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 这题是不用查是否连通的 题意也有点模糊, 我的理解是给出一堆"贝壳", 然后找出贝壳里可以连成一串的输出 利用递归, 层层往里搜, 搜到一个就删一个. 因为可能给出两个一模一样的"贝壳", 所以map数组不是单纯的0和1 输出利用了递归的特点, 算是方便了把 AC代码如下:#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXN 101 int T, N; int map[MAXN+1][MAXN+1]; int degree[MAXN+1]; int mark, cas = 0; //初始化及输入: void init() { memset(degree, 0, sizeof(degree)); for(int i = 1; i <= MAXN; i++) for(int j = 1 ; j <= MAXN; j++) map[i][j] = 0; for(int i = 1; i <= N; i++) { int t1, t2; scanf("%d%d", &t1, &t2); map[t1][t2] = map[t1][t2] + 1; map[t2][t1] = map[t2][t1] + 1; degree[t1] = degree[t1] + 1; degree[t2] = degree[t2] + 1; } } void euler(int v) { for(int i = 1; i <= MAXN; i++) { if(map[v][i] != 0) { map[v][i] = map[v][i] - 1; map[i][v] = map[i][v] - 1; euler(i); printf("%d %d\n", i, v); } } } void solve() { mark = 1; int start; int max = 0; for(int i = 1; i <= MAXN; i++) { if(degree[i] % 2 == 1) { mark = 0; break; } else if(degree[i] > max) { max = degree[i]; start = i; } } if(mark == 0) printf("some beads may be lost\n"); else euler(start); } int main() { scanf("%d", &T); while(T--) { scanf("%d", &N); init(); printf("Case #%d\n", ++cas); solve(); if(T != 0) printf("\n"); } }