Problem UVA10054-The Necklace

Time Limit: 3000 mSec

Problem Description

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
 
题解:比较经典的欧拉回路的题目,将颜色看作节点,珠子相当于连接两个颜色的边,由于珠子可以翻转,因此是无向图。
   无向图欧拉回路存在的充要条件:连通并且所有点的度数均为偶数。
   此题数据直接保证了连通,检查连通性很容易,并查集即可。为了输出一条欧拉回路,采用套圈算法,从任意一个节点出发dfs,走不下去了就回溯,回溯过程                    中逆向输出节点即可。
 
 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); int n, iCase;
int deg[maxn], gra[maxn][maxn]; void dfs(int u)
{
for (int i = ; i <= ; i++)
{
if (gra[u][i])
{
gra[u][i]--;
gra[i][u]--;
dfs(i);
cout << i << " " << u << endl;
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int T;
cin >> T;
while (T--)
{
cin >> n;
cout << "Case #" << ++iCase << endl;
memset(deg, , sizeof(deg));
memset(gra, , sizeof(gra));
int u, v;
for (int i = ; i < n; i++)
{
cin >> u >> v;
gra[u][v]++, gra[v][u]++;
deg[u]++, deg[v]++;
}
bool ok = true;
for (int i = ; i <= ; i++)
{
if (deg[i] % )
{
ok = false;
break;
}
}
if (!ok)
{
cout << "some beads may be lost" << endl;
}
else
{
dfs(v);
}
if (T)
cout << endl;
}
return ;
}

UVA10054-The Necklace(无向图欧拉回路——套圈算法)的更多相关文章

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

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

  2. UVA-10054 The Necklace (欧拉回路)

    题目大意:有n个珠子,珠子两边的颜色已知,问能否连成一条项链.(两个珠子可以项链当且仅当一个珠子的一边颜色与另一个珠子的另一边颜色相同). 题目分析:欧拉回路.将颜色视作节点,珠子当做边,问题变成了找 ...

  3. UOJ 117 欧拉回路(套圈法+欧拉回路路径输出+骚操作)

    题目链接:http://uoj.ac/problem/117 题目大意: 解题思路:先判断度数: 若G为有向图,欧拉回路的点的出度等于入度. 若G为无向图,欧拉回路的点的度数位偶数. 然后判断连通性, ...

  4. UVA10054 The Necklace

    UVA10054 The Necklace 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18806 [思路] 欧拉回路 ...

  5. SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...

  6. UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)

    CALCULATOR CONUNDRUM   Alice got a hold of an old calculator that can display n digits. She was bore ...

  7. UVA 11549 Calculator Conundrum (Floyd判圈算法)

    题意:有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 思路:这个题一定会出现 ...

  8. leetcode202(Floyd判圈算法(龟兔赛跑算法))

    Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number ...

  9. Floyd判圈算法

    Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defi ...

随机推荐

  1. gradle插件从3.2.0升级到3.2.1后报错Error: Cannot create directory 项目目录\thirdlib\build\intermediates\packaged_res\debug\drawable

    报错信息如下:  解决方案: 删除thirdlib\build目录,然后重新编译. 但是紧接着又会报类似的错误,只不过build目录变成其他module的了. 所以,先clear build,然后再重 ...

  2. 带着萌新看springboot源码05

    上一节走了一遍从浏览器发出请求到得到向页面的流程,基本的功能是已经实现了.但是现在啊,我想自定义一个拦截器(拦截器可以做用户登录验证,如果登录了,就让你通过,如果没有登录,就重定向登录页面),这里就不 ...

  3. Typora中的Markdown教程

    Tutorial of markdown in Typora 工欲善其事,必先利其器 如上所说,这里给大家安利一款高BIG的利器Typora,这是一款文艺青年(装逼)必备的用于编写markdown的打 ...

  4. 15分钟在阿里云Kubernetes服务上快速建立Jenkins X Platform并运用GitOps管理应用发布

    本文主要介绍如何在阿里云容器服务Kubernetes上快速安装部署Jenkins X Platform并结合demo实践演示GitOps的操作流程. 注意:本文中使用的jx工具.cloud-envir ...

  5. 第2章 术语 - Identity Server 4 中文文档(v1.0.0)

    规范.文档和对象模型等都使用特定的术语来表述. 2.1 IdentityServer IdentityServer是OpenID Connect提供程序 - 它实现OpenID Connect和OAu ...

  6. Java多线程之---用 CountDownLatch 说明 AQS 的实现原理

    本文基于 jdk 1.8 . CountDownLatch 的使用 前面的文章中说到了 volatile 以及用 volatile 来实现自旋锁,例如 java.util.concurrent.ato ...

  7. 配置多个 git 账号的 ssh密钥

    背景 在工作中,我们通常会以 ssh 的方式配置公司的 git 账号,但是平时也会使用 github 管理自己的项目.因此,我们需要为自己的 github 创建一个新的 git 账号,这就需要生成新的 ...

  8. Java开发笔记(十三)利用关系运算符比较大小

    前面在<Java开发笔记(九)赋值运算符及其演化>中提到,Java编程中的等号“=”表示赋值操作,并非数学上的等式涵义.Java通过等式符号“==”表示左右两边相等,对应数学的等号“=”: ...

  9. Java开发笔记(五十)几种开放性修饰符

    前面介绍子类继承父类的时候,提到了public(公共)和private(私有)两个修饰符,其中public表示它所修饰的实体是允许外部访问的:而private表示它所修饰的实体不允许外部访问,只能在当 ...

  10. Win10系统给文件夹添加备注

    在Win10系统中,相信大多用户都没有看到过文件或者是文件夹上有备注信息.下面给大家分享下在Win10系统中给文件夹或文件添加备注的方法.在添加备注之前,首先我们要在需要显示备注的文件夹中显示&quo ...