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. Kafka并不难学

    1.为什么写这本书? 我建立了一个qq群,有很多人在群里面学习和交流,经常有人问我一些Kafka的知识,我可以感受他们对技术的热情,这是一群刚走出校门,或者工作经验较少,又或是一些转型的开发新人,他们 ...

  2. 爬虫协议 Tobots

    一.简介 Robots 协议(也称为爬虫协议.机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过 Robots 协议告诉搜索引擎哪些页面可以抓取 ...

  3. JDBC 连接池的两种方式——dbcp & c3p0

    申明:本文对于连接资源关闭采用自定义的 JDBCUtils 工具: package com.test.utils; import java.sql.Connection; import java.sq ...

  4. Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息

    使用Magicodes.WeiChat,可以很方便的获取到微信用户的信息.在使用OAuth 2.0之前,你先需要做以下操作: 1)在开发者中心修改[网页授权获取用户基本信息],在弹出的界面输入自己的根 ...

  5. Spring Boot(十四)RabbitMQ延迟队列

    一.前言 延迟队列的使用场景:1.未按时支付的订单,30分钟过期之后取消订单:2.给活跃度比较低的用户间隔N天之后推送消息,提高活跃度:3.过1分钟给新注册会员的用户,发送注册邮件等. 实现延迟队列的 ...

  6. ROS笔记2 构建一个package

    构建package catkin_make 可以理解为一个集成了CMake和make的命令行工具 通常的cmake构建如下 # In a CMake project $ mkdir build $ c ...

  7. 第31章 日志 - Identity Server 4 中文文档(v1.0.0)

    IdentityServer使用ASP.NET Core提供的标准日志记录工具.Microsoft文档有一个很好的介绍和内置日志记录提供程序的描述. 我们大致遵循Microsoft使用日志级别的指导原 ...

  8. ASP.net core 使用UEditor.Core 实现 ueditor 上传功能

    ASP.net core 使用UEditor.Core 实现 ueditor 上传功能 首先通过nuget 引用UEditor.Core,作者github:https://github.com/bai ...

  9. mongoDB连接数据库

    package mongod; import java.util.List; import java.util.ArrayList; import org.bson.types.*; import c ...

  10. 异步加载js的三种方法

    js加载时间线 : 它是根据js出生的那一刻开始记录的一系列浏览器按照顺序做的事,形容的就是加载顺序,可以用来优化什么东西,理论基础,背下来. 1.创建Document对象,开始解析web页面.解析H ...