UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=995
Problem D: 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
这题就是判断是否存在欧拉回路。
每个点的度数必须为偶数,而且连通。
把颜色当成一个点。
递归打印路径。
//============================================================================
// Name : UVA.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#include <vector>
using namespace std;
const int MAXN=;
int F[];
int find(int x)
{
if(F[x]==-)return x;
else return F[x]=find(F[x]);
}
void bing(int x,int y)
{
int t1=find(x);
int t2=find(y);
if(t1!=t2)F[t1]=t2;
}
int num[];
int G[][];
void Traverse(int u)
{
for(int v=;v<=;v++)
if(G[u][v]>)
{
G[u][v]--;
G[v][u]--;
Traverse(v);
printf("%d %d\n",v,u);
}
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int T;
int n;
scanf("%d",&T);
int iCase=;
while(T--)
{
if(iCase>)printf("\n");
iCase++;
scanf("%d",&n);
int u,v;
memset(F,-,sizeof(F));
memset(num,,sizeof(num));
memset(G,,sizeof(G));
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
num[u]++;
num[v]++;
bing(u,v);
G[u][v]++;
G[v][u]++;
}
bool flag=true;
int temp=-;
for(int i=;i<=;i++)
{
if(num[i]==)continue;
if(num[i]%)
{
flag=false;
break;
}
if(temp==-)
{
temp=find(i);
continue;
}
if(temp!=find(i))
{
flag=false;
break;
}
}
printf("Case #%d\n",iCase);
if(!flag)
{
printf("some beads may be lost\n");
continue;
}
for(int i=;i<=;i++)
if(num[i]!=)
{
u=i;
break;
}
Traverse(u);
}
return ;
}
UVA 10054 The Necklace(欧拉回路,打印路径)的更多相关文章
- UVA 10054 the necklace 欧拉回路
有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...
- Uva 10054 欧拉回路 打印路径
看是否有欧拉回路 有的话打印路径 欧拉回路存在的条件: 如果是有向图的话 1.底图必须是连通图 2.最多有两个点的入度不等于出度 且一个点的入度=出度+1 一个点的入度=出度-1 如果是无向图的话 1 ...
- 【欧拉回路】UVA - 10054 The Necklace
题目大意: 一个环被切割成了n个小块,每个小块有头尾两个关键字,表示颜色. 目标是判断给出的n个小块能否重构成环,能则输出一种可行解(按重构次序输出n个色块的头尾颜色).反之输出“some beads ...
- UVA 1626 区间dp、打印路径
uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...
- UVA 624 (0 1背包 + 打印路径)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...
- UVA 531 - Compromise(dp + LCS打印路径)
Compromise In a few months the European Currency Union will become a reality. However, to join th ...
- uva 10054 The Necklace(欧拉回路)
The Necklace My little sister had a beautiful necklace made of colorful beads. Two successive beads ...
- UVa 10054 The Necklace(无向图欧拉回路)
My little sister had a beautiful necklace made of colorful beads. Two successive beads in the neckla ...
- UVA 10054 The Necklace (无向图的欧拉回路)
本文链接:http://www.cnblogs.com/Ash-ly/p/5405904.html 题意: 妹妹有一条项链,这条项链由许多珠子串在一起组成,珠子是彩色的,两个连续的珠子的交汇点颜色相同 ...
随机推荐
- Android开发之消息机制
转:http://stackvoid.com/introduction-to-Message-Handler-in-Android/ http://blog.dreamtobe.cn/2016/03/ ...
- C#调用java程序
前言: 最近跟项目组的人合作一个项目,由于之前我用的是java写的一个与android通信的程序,现在另一个同事来编写界面程序,由于C#编写起来比较方便,而我又不想重新写之前java的那段代码,于是需 ...
- setBackgroundDrawable和setBackgroundColor的用法
1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getResources().getDrawable(R.drawa ...
- UVa 11181 (条件概率) Probability|Given
题意: 有n个人买东西,第i个人买东西的概率为Pi.已知最终有r个人买了东西,求每个人买东西的概率. 分析: 设事件E为r个人买了东西,事件Ei为第i个人买了东西.所求为P(Ei|E) = P(EiE ...
- HDU 1695 (莫比乌斯反演) GCD
题意: 从区间[1, b]和[1, d]中分别选一个x, y,使得gcd(x, y) = k, 求满足条件的xy的对数(不区分xy的顺序) 分析: 虽然之前写过一个莫比乌斯反演的总结,可遇到这道题还是 ...
- ionic preparing for ios 9
http://blog.ionic.io/preparing-for-ios-9/ Preparing for iOS 9 Tweet By Max on September 12, 2015 i ...
- cocos2d_x 问题汇总
1.生成so文件时,报“No rule to make target ”错误 解决方法:将.\xxx[appname]\proj.android\obj\local\armeabi\objs中的文件全 ...
- angular依赖注入的理解(转)
使用过java进行开发的人肯定知道大名鼎鼎的spring框架,对于spring的IOC肯定也有所了解,通过配置文件定义好bean之后,如果需要使用这些bean,不需要自己去实例化,而是跟spring这 ...
- wave文件(*.wav)格式、PCM数据格式
1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit stereo: 每秒钟有 44100 次采 ...
- C#判断用户是否使用微信浏览器,并据此来显示真实内容或二维码
平时我们看一些网页的时候会发现这样的功能:有的页面只能在微信里访问,如果在电脑上访问就只显示当前地址的二维码.这个用C#怎么实现呢?我们结合代码来看看. 首先,我们需要先判断用户使用的是什么浏览器,这 ...