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结束的条件是节点没有可走的边,回溯,回溯的时候若遇到节点还有可走的边,再去递归那条边,使得所有边都被访问过)

代码

 #include<bits/stdc++.h>
using namespace std;
int Map[][],Du[],F[];
int Find(int x)
{
return F[x]==x?x:F[x]=Find(F[x]);
}
void dfs(int u)
{
for(int v=;v<=;v++)
if(Map[u][v]>)//可以走的边
{
Map[u][v]--;//边删掉
Map[v][u]--;
dfs(v);
printf("%d %d\n",v,u);//逆序输出
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,t,u,v;
scanf("%d",&t);
for(int k=;k<=t;k++)
{
if(k!=)printf("\n");
printf("Case #%d\n",k);
scanf("%d",&n);
memset(Du,,sizeof(Du));
memset(Map,,sizeof(Map));
for(int i=;i<=;i++)
F[i]=i;
for(int i=;i<=n;i++)
{
scanf("%d%d",&u,&v);
Du[u]++;Du[v]++;//度数
Map[u][v]++;Map[v][u]++;//边
int fu=Find(u);
int fv=Find(v);
if(fu!=fv)
F[fu]=fv;
}
int flag=;
for(int i=;i<=;i++)
{
if(Du[i]==)continue;
if(Du[i]%==||Find(i)!=Find(u))
{
flag=;break;
}
}
if(flag)dfs(u);
else printf("some beads may be lost\n"); }
return ;
}

UVa 10054 The Necklace(无向图欧拉回路)的更多相关文章

  1. UVA 10054 The Necklace (无向图的欧拉回路)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5405904.html 题意: 妹妹有一条项链,这条项链由许多珠子串在一起组成,珠子是彩色的,两个连续的珠子的交汇点颜色相同 ...

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

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. uva 10054 The Necklace(欧拉回路)

    The Necklace  My little sister had a beautiful necklace made of colorful beads. Two successive beads ...

  4. UVa 10054 The Necklace【欧拉回路】

    题意:给出n个珠子,珠子颜色分为两半,分别用1到50之间的数字表示, 现在给出n个珠子分别的颜色,问是否能够串成一个环.即为首尾相连,成为一个回路 判断是否构成一个环,即判断是否为欧拉回路,只需要判断 ...

  5. UVa 10054 : The Necklace 【欧拉回路】

    题目链接 题目大意:我的妹妹有一串由各种颜色组成的项链. 项链中两个连续珠子的接头处共享同一个颜色. 如上图, 第一个珠子是green+red, 那么接这个珠子的必须以red开头,如图的red+whi ...

  6. uva 10054 The Necklace 拼项链 欧拉回路基础应用

    昨天做了道水题,今天这题是比较水的应用. 给出n个项链的珠子,珠子的两端有两种颜色,项链上相邻的珠子要颜色匹配,判断能不能拼凑成一天项链. 是挺水的,但是一开始我把整个项链看成一个点,然后用dfs去找 ...

  7. UVA 10054 the necklace 欧拉回路

    有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...

  8. UVa 10054 The Necklace BFS+建模欧拉回路

    算法指南 主要就是建立欧拉回路 #include <stdio.h> #include <string.h> #include <iostream> #includ ...

  9. 【欧拉回路】UVA - 10054 The Necklace

    题目大意: 一个环被切割成了n个小块,每个小块有头尾两个关键字,表示颜色. 目标是判断给出的n个小块能否重构成环,能则输出一种可行解(按重构次序输出n个色块的头尾颜色).反之输出“some beads ...

随机推荐

  1. Linux 远程登录配置

    如果使用FlashFxp的sftp无法登陆,提示SSH 错误: 协商认证模式失败,也同样这样操作 修改你的ssh配置文件 配置文件路径如:/etc/ssh/sshd_config PasswordAu ...

  2. jquery左侧菜单

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. html中,纯数字或纯英文的一串字符超出父容器不会折行显示,如何解决?

    这种情况在软件使用过程中一般不会出现,只有测试人员在测试的时候手比较贱会给你弄一个这种数据,当然这也算是bug吧. 如图:“经营范围”的值严重超出父容器长度,并且没有像“服务信息”一样折行显示.这种情 ...

  4. git 恢复到旧版本命令

    1.第一步:找到你想恢复到的版本号:可以在git提交日志中查看-> 找到版本号,复制下来,在git项目根目录下打开git命令窗口: 输入:git reset --hard xxxxxxxxxxx ...

  5. Hibernate 再接触 关系映射 一对一单向外键关联

    对象之间的关系 数据库之间的关系只有外键 注意说关系的时候一定要反面也要说通 CRUD 数据库之间设计 主键关联 单向的外键关联 中间表 一对一单向外键关联 Husband.java package ...

  6. python 如何注释

    一.单行注释     单行注释以#开头,例如:    print 6 #输出6 二.多行注释     (Python的注释只有针对于单行的注释(用#),这是一种变通的方法)     多行注释用三引号' ...

  7. 基于ceph快照快速回滚openstack上的虚拟机

    查看虚拟机ID 1 2 [root@node1 ~]# nova list --all | grep wyl | dc828fed-1c4f-4e5d-ae84-795a0e71eecc | wyl ...

  8. tensorflow 指定使用gpu处理,tensorflow占用多个GPU但只有一个在跑

    我们在刚使用tensorflow的过程中,会遇到这个问题,通常我们有多个gpu,但是 在通过nvidia-smi查看的时候,一般多个gpu的资源都被占满,但是只有一个gpu的GPU-Util 和 21 ...

  9. 关于ubuntu14.04,忘记root密码解决方案(经测试,内核3.13和4.2可行)。

    网上已经有很多方案啦,我这里就不画蛇添足,直接上链接: http://jingyan.baidu.com/article/c843ea0b9e851077931e4aea.html 注意,有些机子没有 ...

  10. 2-glance 部署

    1. mysql 创建数据库和用户 create database glance; grant all privileges on glance.* to 'glance'@'localhost' i ...