UVaOJ10054---The Necklace

来源:互联网 发布:excel一列数据自动复制 编辑:程序博客网 时间:2024/06/08 05:31

10054 - The Necklace

Time limit: 3.000 seconds

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 ( $5 \leN \le 1000$) 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 $1 \le i \le N ­ 1$, the second integer on line imust 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



Miguel Revilla 
2000-12-28
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int M = 55;int n,flag,cnt,colournum,b;int mapp[M][M];int num[M];bool visit[M];//判断是否能构成欧拉通路 从一点出发能够到达的点的个数用 cnt 表示void dfs(int  a){    visit[a] = true;    cnt++;    for(int i = 0; i < 51; i++)    {        if(!visit[i] && mapp[a][i])        dfs(i);    }    return ;}void euler( int a){    for(int i = 1; i < 51; i++)    {        if(mapp[a][i])        {            mapp[a][i]--;            mapp[i][a]--;            euler(i);            cout << i << ' ' << a << endl;        }    }    return ;}int main(){    int ncase;    cin >> ncase;    for(int j = 0; j < ncase; j++)    {        if(j) cout << endl;        memset(mapp,0,sizeof(mapp));        memset(num,0,sizeof(num));        memset(visit,false,sizeof(visit));        flag = 0;        cin >> n;        for(int i = 0; i < n; i++)        {            int x,y;            cin >> x >> y;            mapp[x][y]++;            mapp[y][x]++;            num[x]++;            num[y]++;        }        colournum = 0;        //判断点的度数是否有奇度  并计算图中点的个数        for(int i = 1; i < 51; i++)        {            if(num[i])  {colournum++; b = i;}            if(num[i]%2) { flag = 1; break;}        }        cnt = 0;        cout << "Case #" << j+1 << endl;        if(!flag)        {            dfs(b);            // 如果一条通路不能遍历所有的点 则肯定构不成欧拉回路            if(cnt != colournum)            cout << "some beads may be lost" << endl;            else            euler(b);        }        else        cout << "some beads may be lost" << endl;    }    return 0;}


原创粉丝点击