UVA 10054 The Necklace
My Little sister had a beautiful necklace made of colorful beads. Successive beads in the necklace gkfx a common color at their meeting point. The figure below shows a segment of the necklace:
But, alas! One day, the necklace is torn and the beads were all scattered on the floor. My sister did his best to recollect all the beads from the floor, but she isn't sure whether she was able to collect all of them. Now, she had come to me for help. She wants to know whether it's possible to make a necklace using all the beads she's have in the same the it's her original Neckl ACE is made and if so in which order the bids must is 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 all test case contains an integer N () giving the number of beads my sister is able to colle Ct. Each of the next N lines contains and integers describing the colors of a bead. Colors is represented by integers ranging from 1 to 50.
Output
For each test, the input first output the test case number as shown in the sample output. Then if you apprehend this some beads may is lost just print the sentence 'some beads' be lost itself. Otherwise, printN lines with a single bead description on each line. Each bead description consists of both integers giving the colors of its and ends. For, the second integer in lineI must is the same as the first integer on line i + 1. Additionally, the second integer on lineN must is equal to the first integer in line 1. Since There is many solutions, any one of the them is acceptable.
Print a blank line between successive test cases.
Sample Input
251 22 33 44 55 652 12 23 43 12 4
Sample Output
Case #1some Beads is lost case #22 11 33 44 22 2
Title: Give a bunch of beads, each bead has two colors, asked to determine whether the beads can be linked to a ring necklace.
Problem solving: Typical Euler circuit problems, satisfying:
1, a bit of the degree to be equal to the degree;
2, some of the Unicom
The output should be aware of the point itself forming a ring
Like what:
1-2
2-3
3-1
2-4
4-2
Euler loops require reverse output.
#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) {//Reverse output for (int i = 0; i <; 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 < i++) {Num[i] = i;} for (int i = 0; i < n; i++) {scanf ("%d%d", &a, &b); vis[a]++;vis[b]++;//statistical degree gra[a][b]++;gra[b][a]++;//Build Diagram num[ Get (a)] = get (b); Unicom}int begin = 0;for (int i = 0; i <; i++) {if (Vis[i] && get (i) = = i) {//If the degree is nonzero and the starting point for the ring begins = I;break;} }int flag = 0;for (int i = 0; i <; i++) {flag + vis[i]% 2; if (vis[i] && begin! = Get (i)) {//Not connected flag++;} }printf ("Case #%d\n", cnt++), if (flag) printf ("Some beads is lost\n"), else print (begin), if (T) printf ("\ n");} return 0;}
UVA 10054 The Necklace (Euler circuit)