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. Java基础20:Java8新特性终极指南

    更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...

  2. windows下使用jenkins持续集成.net项目

    前言 随着微服务的兴起,原先一个庞大的项目,被切分一个个功能独立的微服务,虽然使得业务系统的扩展性和维护性得到提升,但是也加大了维护人员的工作量.有的系统由成百上千个微服务组成,如果每次有修改,都要手 ...

  3. springboot情操陶冶-jmx解析

    承接前文springboot情操陶冶-@Configuration注解解析,近期笔者接触的项目中有使用到了jmx的协议框架,遂在前文的基础上讲解下springboot中是如何整合jmx的 知识储备 J ...

  4. 开源分布式数据库SequoiaDB在去哪儿网的实践

    编者注: 中国的数据库行业也迎来了一波新的热点事件.分布式数据库这块新消息不断,也让大家开始关注中国的分布式数据库.首先是短短一周内,Pingcap和SequoiaDB巨杉数据库陆续宣布了C轮的数千万 ...

  5. VisualStudio,用C#写的一个开源移动APP,资产管理类项目SmoSec

    继SmoOne之后,Smobiler团队又推出一款用C#开发的APP开源项目. 这款开源项目名为SmoSec,目前包含资产管理.耗材管理两大类. 并且,未来会不断迭代,持续增加盘点.标签打印和仓库管理 ...

  6. .NET Core 如何使用Session

    第一步先注册session: 在Startup.cs文件中的ConfigureServices方法中添加: services.AddSession(); 在Startup.cs文件中的Configur ...

  7. [PHP]命令执行函数的区别

    <?php $cmd="ps aux|grep php-fpm"; $res=exec($cmd,$o); var_dump($o);//数组形式返回,每行一个元素 var_ ...

  8. 谈谈Mysql主从同步延迟分析及解决方案

    一.MySQL的数据库主从复制原理 MySQL主从复制实际上基于二进制日志,原理可以用一张图来表示: 分为四步走: 1. 主库对所有DDL和DML产生的日志写进binlog: 2. 主库生成一个 lo ...

  9. MySQL Err(1024):Lock wait timeout exceeded; try restarting transaction

    查看事务是否占用被锁: SELECT * FROM information_schema.INNODB_TRX;查看里面的 trx_mysql_thread_id字段 show full proces ...

  10. vue结合axios使用入门

    Vue官方推荐的网络通信库不再是vue-resource了,推荐使用axios axios安装 npm: $ npm install axios bower: $ bower install axio ...