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 题意: 妹妹有一条项链,这条项链由许多珠子串在一起组成,珠子是彩色的,两个连续的珠子的交汇点颜色相同 ...
随机推荐
- jQuery--隐藏事件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- android动画坐标定义
这段时间一直在忙Android的项目,总算抽出点时间休息一下,准备把一些项目用到的Android经验分享一下. 在Android开发过程中,经常会碰到Activity之间的切换效果的问题,下面介绍一下 ...
- 【笨嘴拙舌WINDOWS】编码历史
在介绍历史之前,有必要将一个经常使用的词语"标准"解释一下: " 标准是"为了在一定的范围内获得最佳秩序,经协商一致制定并由公认机构批准,共同使用的和重复使用的 ...
- LA 5059 (找规律 SG函数) Playing With Stones
题意: 有n堆石子,两个人轮流取,每次只能取一堆的至少一个至多一半石子,直到不能取为止. 判断先手是否必胜. 分析: 本题的关键就是求SG函数,可是直接分析又不太好分析,于是乎找规律. 经过一番“巧妙 ...
- LA 2797 (平面直线图PLSG) Monster Trap
题意: 平面上有n条线段,一次给出这n条线段的两个端点的坐标.问怪兽能否从坐标原点逃到无穷远处.(两直线最多有一个交点,且没有三线共交点的情况) 分析: 首先说明一下线段的规范相交:就是交点唯一而且在 ...
- BZOJ2151: 种树
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2151 题解:此题=数据备份.喜闻乐见挂链表. 代码: #include<cstdio&g ...
- 《分销系统-原创第一章》之“多用户角色权限访问模块问题”的解决思路( 位运算 + ActionFilterAttribute )
此项目需求就是根据给用户分配的权限,进行相应的权限模块浏览功能,因为项目不是很大,所以权限没有去用一张表去存,我的解决思路如下,希望大家给点建议. 数据库用户表结构如下: 数据库表梳理: BankUs ...
- windows ODBC数据源里没有Oracle的驱动程序
windows ODBC数据源里没有Oracle的驱动程序 直接在“控制面板---管理工具----数据源(ODBC)” 打开数据源配置,发现只有SQLServer的驱动,其他的都没有了. ...
- Oracle数据文件管理
1.数据文件概述 Oracle数据库的数据文件(扩展名为DBF的文件)是用于保存数据库中数据的文件,系统数据.数据字典数据.临时数据.索引数据.应用数据等都物理地存储在数据文件中.用户对数据库中数据的 ...
- apache开源项目--Syncope
Apache Syncope is an Open Source system for managing digital identities in enterprise environments, ...