uva 10054 The Necklace(欧拉回路)
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 ( ) 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 , 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
题目大意:给出一堆珠子,每个珠子有两个颜色,要求判断所给出的珠子是否能连成一个环状的项链。(可以的话要输出)
解题思路:典型的欧拉回路问题,满足1、所有点的入度要等于出度;
2、所有点的联通(这道题目数据没有卡这里)
输出的时候要注意点的自身形成一个环
比如:
1 -> 2
2 -> 3
3 -> 1
2 -> 4
4 -> 2
欧拉回路需要逆序输出。
#include<stdio.h>
#include<string.h>
#define M 52
int num[M];
int map[M][M];
int n; int get_fa(int x){
return num[x] != x?get_fa(num[x]):x;} void print(int k){
for (int i = 0; i < M; i++)
if (map[k][i]){
map[k][i]--;
map[i][k]--;
print(i);
printf("%d %d\n", i , k);
}
} int main(){
int t, bo, k = 1;
int f[M];
scanf("%d" ,&t);
while (t--){
// Init.
memset(f, 0, sizeof(f));
memset(map, 0, sizeof(map));
bo = 0;
for (int i = 0; i < M; i++)
num[i] = i; // Read.
scanf("%d", &n);
for (int i = 0; i < n; i++){
int a, b;
scanf("%d%d", &a, &b);
f[a]++;
f[b]++;
map[a][b]++;
map[b][a]++;
num[get_fa(a)] = get_fa(b);
} // Find.
int god = 0;
for (int i = 0; i < M; i++)
if (f[i] && get_fa(i) == i)
{
god = i;
break;
} // Judge.
for (int i = 0; i < M; i++){
bo += f[i] % 2;
if (f[i] && god != get_fa(i))
bo++;
} // Printf.
printf("Case #%d\n", k++);
if (bo > 0)
printf("some beads may be lost\n");
else
print(god);
if (t)
printf("\n");
}
return 0;}
uva 10054 The Necklace(欧拉回路)的更多相关文章
- UVA 10054 the necklace 欧拉回路
有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...
- UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 10054 The Necklace(无向图欧拉回路)
My little sister had a beautiful necklace made of colorful beads. Two successive beads in the neckla ...
- uva 10054 The Necklace 拼项链 欧拉回路基础应用
昨天做了道水题,今天这题是比较水的应用. 给出n个项链的珠子,珠子的两端有两种颜色,项链上相邻的珠子要颜色匹配,判断能不能拼凑成一天项链. 是挺水的,但是一开始我把整个项链看成一个点,然后用dfs去找 ...
- UVa 10054 The Necklace【欧拉回路】
题意:给出n个珠子,珠子颜色分为两半,分别用1到50之间的数字表示, 现在给出n个珠子分别的颜色,问是否能够串成一个环.即为首尾相连,成为一个回路 判断是否构成一个环,即判断是否为欧拉回路,只需要判断 ...
- UVa 10054 The Necklace BFS+建模欧拉回路
算法指南 主要就是建立欧拉回路 #include <stdio.h> #include <string.h> #include <iostream> #includ ...
- 【欧拉回路】UVA - 10054 The Necklace
题目大意: 一个环被切割成了n个小块,每个小块有头尾两个关键字,表示颜色. 目标是判断给出的n个小块能否重构成环,能则输出一种可行解(按重构次序输出n个色块的头尾颜色).反之输出“some beads ...
- UVA 10054 The Necklace (无向图的欧拉回路)
本文链接:http://www.cnblogs.com/Ash-ly/p/5405904.html 题意: 妹妹有一条项链,这条项链由许多珠子串在一起组成,珠子是彩色的,两个连续的珠子的交汇点颜色相同 ...
- UVA 10054 The Necklace 转化成欧拉回路
题意比较简单,给你n个项链碎片,每个碎片的两半各有一种颜色,最后要把这n个碎片串成一个项链,要求就是相邻碎片必须是同种颜色挨着. 看了下碎片总共有1000个,颜色有50种,瞬间觉得普通方法是无法在可控 ...
随机推荐
- hdu4717 The Moving Points 三分法
题意:坐标系上有n个点,每个点的坐标和移动方向速度告诉你,速度方向都是固定的.然后要求一个时刻,使得这个时刻,这些点中最远的距离最小. 做法:三分法,比赛的时候想不到.考虑两个点,如果它们走出来的路径 ...
- Mac OS X中报:java.io.UnixFileSystem.createFileExclusively(Native Method)的简单原因
这个博客太简单了!想到可能有其它朋友也遇到这个问题,就记录一下. 今天把一个之前在Windows上的Java项目放到Mac OS X上执行,本来认为应该非常easy的事情,结果还是报: Excepti ...
- 第一次测试HTML和CSS
1.HTML(Hyper Text Markup Languange)超文本标记语言.HTML文件扩展名通常是:htm和html. <html> <head> <titl ...
- Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)
描述Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a curren ...
- JS动态获取浏览器宽度和高度
$(window).resize(function() { var width = $(this).width(); var height = $(this).height(); });
- BIEE在creating domain步骤停止的解决的方法
1.错误现象: biee11g creating domain csf entries will not be parsed since the adminserver is unreachable ...
- linux_coom _ Linux文件比较,文本文件的交集、差集与求差
交集和差集操作在集合论相关的数学课上经常用到,不过,在Linux下 对文本进行类似的操作在某些情况下也很有用. comm命令 comm命令可以用于两个文件之间的 比较,它有一些选项可以用来调整输出,以 ...
- Java设计模式偷跑系列(十二)组合模式建模和实现
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39828653 组合模式(Composite):组合模式有时又叫部分-总体模式.将对象组合成 ...
- Hadoop之环境搭建
初学Hadoop之环境搭建 阅读目录 1.安装CentOS7 2.安装JDK1.7.0 3.安装Hadoop2.6.0 4.SSH无密码登陆 本文仅作为学习笔记,供大家初学Hadoop时学习参考. ...
- JS工具库之Lodash
破狼 JavaScript工具库之Lodash 2015-04-11 16:08 by 破狼, 235 阅读, 2 评论, 收藏, 编辑 你还在为JavaScript中的数据转换.匹配.查找等烦恼吗? ...