UVA 10054 欧拉回路

http://vjudge.net/contest/view.action?cid=48251#overview

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 

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
题目大意:一条项链有一些珠子组成,这些珠子都有两种颜色,拼成项链是一个珠子的右半边的颜色要和它右边珠子的左半边一个颜色,而它左半边的颜色要和它的左边的珠子的右半边颜色一致。现在珠子散了。问用原来的珠子能否回复该项链。

解题思路:如果把珠子看成点,似乎无法把题目化成一个经典的,可以有效解决的问题,但是 如果把每种颜色看成是一个节点,每个珠子的两边有一条有向边,则题目可以转化为欧拉回路问题,在线性时间内解决。

Uva 上的格式要求还是很严格的,最后一组数据输出后不能有空行

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int mat[55][55];
int deep[55];
void init()
{
    memset(mat,0,sizeof mat);
    memset(deep,0,sizeof deep);
}
void euler(int u)
{
    for (int i=1;i<=50;i++){
        while (mat[u][i]){
            mat[u][i]--;
            mat[i][u]--;
            euler(i);
            printf("%d %d\n",i,u);
        }
    }
}
int main()
{
    int t,n,a,b,kase=0;
    scanf("%d",&t);
    while (t--){
        init();
        scanf("%d",&n);
        for (int i=0;i<n;i++){
            scanf("%d%d",&a,&b);
            deep[a]++;deep[b]++;
            mat[a][b]++;mat[b][a]++;
        }
        printf("Case #%d\n",++kase);
        bool flag=0;
        for (int i=1;i<=50;i++){
            if (deep[i]%2==1){flag=1;break;}
        }
        if (flag){
            puts("some beads may be lost");
            if (t) puts("");
            continue;
        }
        int u=1;
        while (deep[u]==0) u++;
        euler(u);
        if (t) puts("");
    }
    return 0;
}


你可能感兴趣的:(UVA 10054 欧拉回路)