zoukankan      html  css  js  c++  java
  • uva 10054 The Necklace 欧拉回路

     

      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
    

    Miguel Revilla
    2000-12-28
     
    判断欧拉回路:
    1.图是否连通    2.是否存在奇点    3.打印欧拉回路
    其中第3步可用dfs类似于后序遍历实现,但为啥前序遍历不行呢?不懂。  AC代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    
    #define N 55
    int g[N][N];
    int n;
    struct edge{
        int from, to;
    };
    
    vector<edge> ans;
    
    void dfs(int s){
        for(int i=1; i<=50; i++) if(g[s][i]){
            g[s][i]--;  g[i][s]--;
            dfs(i);
            ans.push_back((edge){s,i});
        }
    }
    
    int main(){
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int cas,tt=1;
        scanf(" %d",&cas);
        while(cas--){
            int i,j;
            int u,v,degree[N];
            scanf(" %d",&n);
            memset(g, 0, sizeof(g));
            memset(degree, 0, sizeof(degree));
            ans.clear();
            for(i=0; i<n; i++){
                scanf(" %d %d",&u,&v);
                g[u][v]++;      g[v][u]++;
                degree[u]++;    degree[v]++;
            }
            bool f=false;
            for(i=1; i<=50; i++)
                if(degree[i]&1) {
                    f=true; break;
                }
            dfs(u);
            int m=ans.size();
            if(tt!=1) printf("
    ");
            printf("Case #%d
    ",tt++);
            if(m<n || ans[m-1].from!=ans[0].to || f) printf("some beads may be lost
    ");
            else{
                for(i=m-1; i>-1; i--)
                    printf("%d %d
    ",ans[i].from,ans[i].to);
            }
        }
        return 0;
    }
    
     
  • 相关阅读:
    为什么说2013是PHP年
    wordpress 投稿插件 支持图片上传
    php简易页面内调试技巧
    WordPress中文文档
    百度网盘文件直链
    HOWTO:如何解决安装包在系统“添加/删除”中无法修复或卸载的问题
    InstallShield 2008 终止声明 (EOL)对最终客户意味着什么
    InstallShield 2011新功能试用(10) Express版本
    AdminStudio 9.5 Service Pack 3
    INFO:InstallShield中安装路径变量的区别
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3271793.html
Copyright © 2011-2022 走看看