• UVA 10054 The Necklace(欧拉回路,打印路径)


    题目链接:

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=995

    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 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 le
N 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 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
    

    这题就是判断是否存在欧拉回路。

    每个点的度数必须为偶数,而且连通。

    把颜色当成一个点。

    递归打印路径。

    //============================================================================
    // Name        : UVA.cpp
    // Author      : 
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <map>
    #include <vector>
    using namespace std;
    const int MAXN=1010;
    int F[60];
    int find(int x)
    {
        if(F[x]==-1)return x;
        else return F[x]=find(F[x]);
    }
    void bing(int x,int y)
    {
        int t1=find(x);
        int t2=find(y);
        if(t1!=t2)F[t1]=t2;
    }
    int num[60];
    int G[60][60];
    void Traverse(int u)
    {
        for(int v=1;v<=50;v++)
            if(G[u][v]>0)
            {
                G[u][v]--;
                G[v][u]--;
                Traverse(v);
                printf("%d %d
    ",v,u);
            }
    }
    int main()
    {
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        int T;
        int n;
        scanf("%d",&T);
        int iCase=0;
        while(T--)
        {
            if(iCase>0)printf("
    ");
            iCase++;
            scanf("%d",&n);
            int u,v;
            memset(F,-1,sizeof(F));
            memset(num,0,sizeof(num));
            memset(G,0,sizeof(G));
            for(int i=0;i<n;i++)
            {
                scanf("%d%d",&u,&v);
                num[u]++;
                num[v]++;
                bing(u,v);
                G[u][v]++;
                G[v][u]++;
            }
            bool flag=true;
            int temp=-1;
            for(int i=1;i<=50;i++)
            {
                if(num[i]==0)continue;
                if(num[i]%2)
                {
                    flag=false;
                    break;
                }
                if(temp==-1)
                {
                    temp=find(i);
                    continue;
                }
                if(temp!=find(i))
                {
                    flag=false;
                    break;
                }
            }
            printf("Case #%d
    ",iCase);
            if(!flag)
            {
                printf("some beads may be lost
    ");
                continue;
            }
            for(int i=1;i<=50;i++)
                if(num[i]!=0)
                {
                    u=i;
                    break;
                }
            Traverse(u);
        }
        return 0;
    }
  • 相关阅读:
    CCF201604试题
    CCF201512试题
    CCF201509试题
    CCF201509试题
    CCF201503试题
    CCF201503试题
    CCF201412试题
    CCF201412试题
    CCF201409试题
    CCF201409试题
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3149728.html
Copyright © 2020-2023  润新知