zoukankan      html  css  js  c++  java
  • UVa 10054 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 <= 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

    题意

    给你n串珠子的颜色(两边),问是否能连成一个环

    题解

    1.把每串珠子想成两个相互连接点,变成无向图

    2.并查集判断是否符合无向图欧拉回路的条件:每个点的度数都为偶数

    3.这里注意DFS的时候要逆序输出(DFS结束的条件是节点没有可走的边,回溯,回溯的时候若遇到节点还有可走的边,再去递归那条边,使得所有边都被访问过)

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int Map[55][55],Du[55],F[55];
     4 int Find(int x)
     5 {
     6     return F[x]==x?x:F[x]=Find(F[x]);
     7 }
     8 void dfs(int u)
     9 {
    10     for(int v=1;v<=50;v++)
    11         if(Map[u][v]>0)//可以走的边
    12         {
    13             Map[u][v]--;//边删掉
    14             Map[v][u]--;
    15             dfs(v);
    16             printf("%d %d
    ",v,u);//逆序输出
    17         }
    18 }
    19 int main()
    20 {
    21     //freopen("in.txt","r",stdin);
    22     //freopen("out.txt","w",stdout);
    23     int n,t,u,v;
    24     scanf("%d",&t);
    25     for(int k=1;k<=t;k++)
    26     {
    27         if(k!=1)printf("
    ");
    28         printf("Case #%d
    ",k);
    29         scanf("%d",&n);
    30         memset(Du,0,sizeof(Du));
    31         memset(Map,0,sizeof(Map));
    32         for(int i=1;i<=50;i++)
    33             F[i]=i;
    34         for(int i=1;i<=n;i++)
    35         {
    36             scanf("%d%d",&u,&v);
    37             Du[u]++;Du[v]++;//度数
    38             Map[u][v]++;Map[v][u]++;//
    39             int fu=Find(u);
    40             int fv=Find(v);
    41             if(fu!=fv)
    42                 F[fu]=fv;
    43         }
    44         int flag=1;
    45         for(int i=1;i<=50;i++)
    46         {
    47             if(Du[i]==0)continue;
    48             if(Du[i]%2==1||Find(i)!=Find(u))
    49             {
    50                 flag=0;break;
    51             }
    52         }
    53         if(flag)dfs(u);
    54         else printf("some beads may be lost
    ");
    55 
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    C# MVC模式设置404&500
    jQuery实现滚动条滚动到子元素位置(方便定位)
    Oracle数据库的导出导入
    Oracle之clob字段不能union的问题
    div中内容垂直居中的方法小结
    C#实现Oracle数据库插入clob字段类型数据
    C_结构体_笔记
    Practice_17_01_ABC
    Practice_17_01_GID
    【转】 vim显示行号、语法高亮、自动缩进的设置
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8480679.html
Copyright © 2011-2022 走看看