uva 10054 The Necklace(歐拉迴路)

來源:互聯網
上載者:User

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 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 lines
with 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 

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>#define M 52int num[M];int map[M][M];int n;int get_fa(int x){return num[x] != x?get_fa(num[x]):x;}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, bo, k = 1;int f[M];scanf("%d" ,&t);while (t--){// Init.memset(f, 0, sizeof(f));memset(map, 0, sizeof(map));bo = 0;for (int i = 0; i < M; i++)num[i] = i;        // Read.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]++;            num[get_fa(a)] = get_fa(b);}        // Find.int god = 0;    for (int i = 0; i < M; i++)if (f[i] && get_fa(i) == i){god = i;break;}        // Judge.for (int i = 0; i < M; i++){bo += f[i] % 2;if (f[i] && god != get_fa(i))bo++;}        // Printf.printf("Case #%d\n", k++);if (bo > 0)printf("some beads may be lost\n");elseprint(god);if (t)printf("\n");}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.