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(欧拉回路)的更多相关文章

  1. UVA 10054 the necklace 欧拉回路

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

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

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

  3. UVa 10054 The Necklace(无向图欧拉回路)

    My little sister had a beautiful necklace made of colorful beads. Two successive beads in the neckla ...

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

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

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

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

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

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

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

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

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

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

  9. UVA 10054 The Necklace 转化成欧拉回路

    题意比较简单,给你n个项链碎片,每个碎片的两半各有一种颜色,最后要把这n个碎片串成一个项链,要求就是相邻碎片必须是同种颜色挨着. 看了下碎片总共有1000个,颜色有50种,瞬间觉得普通方法是无法在可控 ...

随机推荐

  1. java 中间String分类

    String a = "aaa"; 用这样的方式的时候java首先在内存中寻找"aaa"字符串.假设有.就把aaa的地址给它 假设没有则创建 String a ...

  2. BZOJ 1010: [HNOI2008]玩具包装toy

    职务地址:http :// www . lydsy . com / JudgeOnline / problem . php ? id = 1010 题目大意:见原题. 算法分析: 设s[i]为c[i] ...

  3. Access Toke调用受保护的API

    ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API 在前一篇博文中,我们使用OAuth的Client Credential Grant授权方式,在服务端 ...

  4. win7或windows server 2008 R2 被远程登录日志记录 系统日志

    事件查看器 → Windows 日志 → 安全 (win7 事件查看器 打开方式 :计算机 右键   → 管理  → 计算机管理 → 系统工具 → 事件查看器 windows server 2008 ...

  5. ArcGIS网络分析之Silverlight客户端路径分析(三)

    原文:ArcGIS网络分析之Silverlight客户端路径分析(三) 首先贴上最终的效果图: a.路径查询 2.最近设施点查询 3.服务区分析 说明: 1.以上的示例使用的数据是随意在ArcMap中 ...

  6. Jquery zTree实例

    zTree[简单介绍] zTree 是利用 JQuery 的核心代码,实现一套能完毕大部分经常使用功能的 Tree 插件 兼容 IE.FireFox.Chrome 等浏览器 在一个页面内可同一时候生成 ...

  7. 让你的Windows不断重启的C语言代码

    原文:让你的Windows不断重启的C语言代码 没有写Linux的原因是因为搞不定Linux下的权限问题,而Windows下基本上使用电脑的用户都是管理员,所以钻个空了,不多说下面是代码#includ ...

  8. 探讨javascript面向对象编程

    (个人blog迁移文章.) 前言: 下面将探讨javascript面向对象编程的知识. 请不要刻意把javascript想成面向对象编程是理所当然的. javascript里面,对象思想不可少,但是不 ...

  9. Java多线程之进程和线程

    在并发编程中有两个基本的概率就是进程和线程.在Java编程中并发编程更多的是关注线程.但是进程也是很重要的. 一个计算机一般会有很多活跃的进程和线程.有一点是没有疑问的在单核系统中,任何时候实际上都是 ...

  10. 华硕K55DR体验 - 显卡就是坑

    朋友拿来电脑,本来他室友已经把他电脑重做完了,但还是卡,非要给我再搞一遍,难道?我就是传说中的大神?咳咳...YY一下,适可而止 华硕K55DR的配置来看,似乎应付CF没什么问题,可是,FPS各种不稳 ...