UVA10054 The Necklace (输出欧拉回路)
2016-08-18 00:34
471 查看
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 gure below shows a segment of the necklace:
But, alas! One day, the necklace was torn and the beads were all scattered over the oor. My sister
did her best to recollect all the beads from the oor, 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 rst line of the input contains the integer
T
.
The rst 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 rst 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
N
1, the second integer
on line
i
must be the same as the rst integer on line
i
+ 1. Additionally, the second integer on line
N
must be equal to the rst integer on line 1. Since there are many solutions, any one of them is
acceptable.
Print a blank line between two successive test cases.
SampleInput
2
5
1 2
2 3
3 4
4 5
5 6
5
2 1
2 2
3 4
3 1
2 4
SampleOutput
Case #1
some beads may be lost
Case #2
2 1
1 3
3 4
4 2
2 2
题解:
一开始就是以为输出一个环,结果发现怎么搞都搞不过。。
后来才知道这是个欧拉回路,我真的智商。。够了。。。
好吧。。
判断一下欧拉回路是否存在,其实需要判断一下图是否联通的,但是这道题数据貌似都是联通的。需要倒着输出的原因是递归,不然你用vector存下来反着输出也行,不过还是没必要。
shared a common color at their meeting point. The gure below shows a segment of the necklace:
But, alas! One day, the necklace was torn and the beads were all scattered over the oor. My sister
did her best to recollect all the beads from the oor, 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 rst line of the input contains the integer
T
.
The rst 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 rst 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
N
1, the second integer
on line
i
must be the same as the rst integer on line
i
+ 1. Additionally, the second integer on line
N
must be equal to the rst integer on line 1. Since there are many solutions, any one of them is
acceptable.
Print a blank line between two successive test cases.
SampleInput
2
5
1 2
2 3
3 4
4 5
5 6
5
2 1
2 2
3 4
3 1
2 4
SampleOutput
Case #1
some beads may be lost
Case #2
2 1
1 3
3 4
4 2
2 2
题解:
一开始就是以为输出一个环,结果发现怎么搞都搞不过。。
后来才知道这是个欧拉回路,我真的智商。。够了。。。
好吧。。
判断一下欧拉回路是否存在,其实需要判断一下图是否联通的,但是这道题数据貌似都是联通的。需要倒着输出的原因是递归,不然你用vector存下来反着输出也行,不过还是没必要。
#include<iostream> using namespace std; #include<cstdio> #include<cstring> #include<vector> const int maxn=2005; int a[maxn][maxn],in[55]; void dfs(int now){ for(int i=1;i<=50;++i){ if(a[now][i]>0){ --a[now][i],--a[i][now]; dfs(i); printf("%d %d\n",i,now); } } } int main() { #ifdef tangge freopen("UVA10054.in","r",stdin); #endif // tangge int n,u,v,Tcase=1,T; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(a,0,sizeof(a)),memset(in,0,sizeof(in)); for(int i=0;i<n;++i){ scanf("%d%d",&u,&v); ++in[u],++in[v]; ++a[u][v],++a[v][u]; } bool J=true; for(int i=1;i<=50;++i){ if(in[i]&1){ J=false;break; } } if(Tcase>=2)putchar(10); printf("Case #%d\n",Tcase++); if(!J) printf("some beads may be lost\n"); else { for(int i=1;i<=50;++i){ if(in[i]>0){ dfs(i);break; } } } } return 0; }
相关文章推荐
- Gson 框架的使用
- HDU 1176 免费馅饼 (动态规划)
- Oak先生-Android的多点触摸之图片放大缩小
- Java垃圾回收小结
- javac编译
- ORACLE 11GR2 RAC的网络规划解析
- 【C语言从入门到懵逼】#2 第一个程序
- C# 配列のチュートリアル tutorial
- 338. Counting Bits
- 设置input光标的位置
- 使用oracle-rdbms-server-11gR2-preinstall在Oracle Linux 6上简化安装Oracle11gR2
- 解决java.net.MalformedURLException: unknown protocol: c问题
- django 关于Pillow的安装使用
- 数学推导______Different Sums( hdu 5847 2016多校第九场)
- HDU 4756 Install Air Conditioning(树形dp+MST)
- ios 关于3d旋转问题
- 17、网络基础
- Glide使用教程
- scala进阶18-隐式转换-隐式参数
- 读书笔记(C++)————【重载操作符与转换】