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. DataRead和DataSet的异同

    第一种解释 DataReader和DataSet最大的区别在于,DataReader使用时始终占用SqlConnection(俗称:非断开式连接),在线操作数据库时,任何对SqlConnection的 ...

  2. 【算法与数据结构专场】BitMap算法基本操作代码实现

    上篇我们讲了BitMap是如何对数据进行存储的,没看过的可以看一下[算法与数据结构专场]BitMap算法介绍 这篇我们来讲一下BitMap这个数据结构的代码实现. 回顾下数据的存储原理 一个二进制位对 ...

  3. MTU MSS PDU SDU

    首先要说两个概念: PDU:协议数据单元,计算机网络各层对等实体间交换的单位叫做PDU,不同层的PDU名称不同 层 名称 应用层 数据 传输层 段 segment 网络层 数据包 package 链路 ...

  4. Spring Boot 2.x(四):整合Mybatis的四种方式

    前言 目前的大环境下,使用Mybatis作为持久层框架还是占了绝大多数的,下面我们来说一下使用Mybatis的几种姿势. 姿势一:零配置注解开发 第一步:引入依赖 首先,我们需要在pom文件中添加依赖 ...

  5. JAVA设计模式——开篇

    设计模式很重要,重要性我就不再复述了.最主要的是,通常我们在写一定量代码后,常用的方法什么的都熟悉后,想再提高代码能力,我找到的最好的方法还是去学习,理解设计模式.不理解设计模式,看一些开源框架和ja ...

  6. 在OQL上使用UPDLOCK锁定查询结果,安全的更新实体数据

    SqlServer查询记录的时候提供多种锁定方式,其中UPDLOCK 的优点是允许您读取数据(不阻塞其它事务)并在以后更新数据,同时确保自从上次读取数据后数据没有被更改.当我们用UPDLOCK来读取记 ...

  7. URL中包含url参数,(文件路径作为参数)

    用encodeURIComponent方法,把路径放在里面,可以防止斜杠被取消. 以下attachfiles是我的一个文件的绝对路径. window.location.href="${pag ...

  8. 多层json的构造,取值,还有使用bootstrap的tree view在前端展示的相关问题

    bootstrap-tree view是一款非常好用的插件,它可以添加任意多层节点,效果如下所示: 使用之前需要在HTML页面添加依赖文件: <link href="bootstrap ...

  9. C#学习笔记---C#操作SQL数据库

    C#操作SQL数据库 Connection(连接)对象 连接字符串: 形式1.”server=;uid=;pwd=;database=” 形式2.”server=;Intergrated Securi ...

  10. 操作DataTable数据,修改某列的值

    DataTable table : DataRow row=table.Rows[i];//DataTable的第i行 row.BeginEdit();//开始编辑行 row["column ...