uva 10054 项链

原题:
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 ≤ N ≤ 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 ≤ i ≤ N1, 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
题目大意:
给一堆珠子,每个珠子分为两头各有一个一种颜色。现在让你把这些珠子串成一个圈,使得项链的两个珠子的相邻两端颜色相同。然后把顺序打印出来~
注意,每个珠子可能有种~

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iomanip>
using namespace std;
int gp[51][51];
int mark[51];
void euler(int head)
{
    for(int i=0;i<=50;i++)
    {
        if(gp[head][i])
        {
            gp[head][i]--;
            gp[i][head]--;;
            euler(i);
            cout<<i<<" "<<head<<endl;//这里注意打印方向不要反
        }
    }
}
void ini()
{
    memset(gp,0,sizeof(gp));
    memset(mark,0,sizeof(mark));
}
int main()
{
    ios::sync_with_stdio(false);
    int a,b,n,k,t=0,e;
    cin>>k;
    while(k--)
    {
        e=1;
        cin>>n;
        ini();
        for(int i=0;i<n;i++)
        {
            cin>>a>>b;
            gp[a][b]++;
            gp[b][a]++;;
            mark[a]++;
            mark[b]++;
        }
        for(int i=0;i<=50;i++)
        if(mark[i]%2)//判断能否成为回一个欧拉回路
        {
            e=0;break;
        }
        if(t)
            cout<<endl;
        if(e)
        {
            cout<<"Case #"<<++t<<endl;
            euler(a);
        }
        else
        {
            cout<<"Case #"<<++t<<endl;
            cout<<"some beads may be lost"<<endl;
        }
    }
    return 0;
}




思路:
拿着刘汝佳的白书随便挑了一个就开始做,挑中这道题。很明显是一个环形一笔画的问题。
刚开始做的时候我把打印的路径都存在一个deque里面,结果居然超时。只能直接打印路径,这里面刘汝佳白书的那个模板不错,注意打印路径是一个环,所以进行euler判断的时候不用先把第一个元素标记过,否则没有办法打印出开头那个珠子。
此外,这里相同的珠子可能有很多个,所以不能光用vis判断,应该在把每个珠子计数,使用过一个以后就gp[a][b]–即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值