The Necklace UVA10054



Problem D: 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 containsTtest cases. The first line of the input contains the integerT.

The first line of each test case contains an integerN($5 \leN \le 1000$) giving the number of beads my sister was able to collect. Each of the nextNlines 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, printNlines 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 lineimust be the same as the first integer on linei+ 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

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



Miguel Revilla
2000-12-28

又是一道无向图的欧拉图问题,首先判定是不是欧拉图,再输出,这里用到了一个算法:

Fleury算法:
    (1)任取v0∈V(G),令P0=v0;
    (2)设Pi=v0e1v1e2...eivi已经行遍,按下面方法来从E(G)-{e1,e2,...,ei}中选
    取ei+1:
    (a)ei+1与vi想关联;
    (b)除非无别的边可供行遍,否则ei+1不应该为Gi=G-{e1,e2,...,ei}中的桥.
    (3)当(2)不能再进行时,算法停止。
例如数据:用电脑把每一步输出,大致理解就是随便找个点然后一它为起点开始遍历,到不能继续时,返回上一层,这是侯输出刚才无法继续遍历的那个店,也就是以他为起点,利用dfs的递归返回时的过程输出,这个算法比回溯快些。
#include<iostream>
#include<cstring>

using namespace std;

int grid[100][100];
int du[100];
int m,tag;

void euler(int pos)
{
    for(int i=1;i<=50;i++)
    {
        if(grid[pos][i]>0)
        {
            grid[pos][i]--;
            grid[i][pos]--;
            euler(i);
            cout<<i<<" "<<pos<<endl;
        }
    }
}

int main()
{
    int n,t;
    cin>>n;
    for(t=1;t<=n;t++)
    {
        memset(grid,0,sizeof(grid));
        memset(du,0,sizeof(du));
        cin>>m;
        int i,j,k;
        for(i=0;i<m;i++)
        {
            cin>>j>>k;
            grid[j][k]++;
            grid[k][j]++;
            du[j]++;
            du[k]++;
        }
        tag=1;
        for(i=1;i<=50;i++)
            if(du[i]%2!=0)
                tag=0;
        cout<<"Case #"<<t<<endl;
        if(tag==0) cout<<"some beads may be lost"<<endl;
        else euler(k);
        if(t<n) cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值