uva10054

来源:互联网 发布:excel 数据截断 编辑:程序博客网 时间:2024/06/05 03:53


 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 theintegerT.

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 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, printN lines with a single bead description on each line. Eachbead 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 thefirst integer on line i + 1. Additionally, the second integer on lineNmust 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


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int O[60],V[60];
int M[60][60],Vis[60][60],m;
int euler(int u)
{
    for(int i=0; i<50; i++)
        if(M[u][i])
        {
            M[u][i]--,M[i][u]--;
            euler(i);
            cout<<i+1<<' '<<u+1<<endl;
        }
}
int find(int u)//必须要判连通
{
    for(int i=0; i<50; i++)
        if(!Vis[u][i]&&M[u][i])
        {
            V[i]=1;
            Vis[u][i]=1;
            find(i);
        }
}
int main()
{
   //  freopen("1.txt","r",stdin);
    int num;
    cin>>num;
    for(int i=0; i<num; i++)
    {
        int N,a,b;
        cin>>N;
        memset(O,0,sizeof(O));
        memset(M,0,sizeof(M));
        memset(V,0,sizeof(V));
        memset(Vis,0,sizeof(Vis));
        for(int j=0; j<N; j++)
        {
            cin>>a>>b;
            M[a-1][b-1]++;
            M[b-1][a-1]++;
            O[a-1]++;
            O[b-1]++;
        }
        int OK=1,s=0,ok;
        V[a-1]=1;
        find(a-1);
        for(int j=0; j<50; j++)
        {
            if(V[j])s++;
            if(O[j])s--;
        }
        if(s)OK=0;
        for(int j=0; j<50; j++)
        {
            if(O[j]%2)
            {
                OK=0;
                break;
            }
            else if(O[j])ok=j;
        }
        m=0;
        if(i)cout<<endl;
        cout<<"Case #"<<i+1<<endl;
        if(OK)euler(ok);
        else cout<<"some beads may be lost"<<endl;
    }
    return 0;
}


Miguel Revilla
2000-12-28