UVA - 10054 - The Necklace (欧拉回路!!)

UVA - 10054

The Necklace

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

 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

Miguel Revilla

2000-12-28

Source

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Graph :: Special Graphs (Others) :: Eulerian
Graph

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Graph :: Special Graphs (Others) :: Eulerian
Graph

Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 5. Graph Theory :: Fundamentals :: Examples

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 2. Data Structures :: Graphs

Root :: Prominent Problemsetters :: Rezaul Alam Chowdhury

Root :: Programming Challenges (Skiena & Revilla) :: Chapter 10

判断欧拉回路的技巧为:所有点的度皆为偶数

这里注意欧拉回路的打印技巧

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int map[55][55];
int num[55];
int n, cas = 1;

void dfs(int x)
{
	for(int i = 1; i <= 50; i++)
		if(map[x][i])
		{
			map[x][i]--; map[i][x]--;
			printf("%d %d\n", x, i);
			dfs(i);
		}
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		memset(map, 0, sizeof(map));
		memset(num, 0, sizeof(num));
		for(int i = 0; i < n; i++)
		{
			int a, b;
			scanf("%d %d", &a, &b);
			map[a][b]++; map[b][a]++;
			num[a]++; num[b]++;
		}
		int flag = 1, max = 0, maxi = 0;
		for(int i=1; i<=50; i++)
		{
			if(num[i] % 2 == 1)
			{ flag = 0; break; }
			else if(num[i] > max)
			{
				max = num[i];
				maxi = i;
			}
		}
		printf("Case #%d\n", cas++);
		if(flag) dfs(maxi);
		else printf("some beads may be lost\n");
		if(T>0) printf("\n");
	}
	return 0;
}
时间: 2024-10-04 00:03:17

UVA - 10054 - The Necklace (欧拉回路!!)的相关文章

uva 10054 The Necklace 欧拉回路

// uva 10054 The Necklace 欧拉回路 // 以颜色为节点,两种颜色互相连着有向边,然后跑一边欧拉回路就ok了 // 这题套了余老师模板书上的欧拉回路,然后就过了 // // 通过这题我了解到了,欧拉回路的基本思想 // 哎,继续练吧... #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat>

UVA 10054 - The Necklace(欧拉回路)

题目链接:点击打开链接 题意:n个珠子,每个珠子的两半由不同的颜色组成. 只有相同的颜色才能接在一起, 问能否组成一个一个项链. 思路:如果将一个珠子看成是一条连接两个顶点的无向边,那么本题就变成了求无向图是否存在欧拉回路.  对于无向图, 如果所有点的度数都是偶数并且图是联通的, 那么就存在欧拉回路.   那么从任意一个点开始走都将走完所有道路并回到起点. 细节参见代码: #include<cstdio> #include<cstring> #include<algorit

UVA 10054 The Necklace (无向图的欧拉回路)

题意: 妹妹有一条项链,这条项链由许多珠子串在一起组成,珠子是彩色的,两个连续的珠子的交汇点颜色相同,也就是对于相邻的两个珠子来说,前一个珠子的末端颜色和后一个珠子的首端颜色相同.有一天,项链断了,珠子洒落了一地,到处都是,妹妹使出浑身解数把地板上能看到的珠子(5-1000)都捡了起来,但是不确定是否收集齐了.给你他妹妹收集的珠子的两端的颜色编号(1 - 50),让你判断是否收集齐了. 思路: 把颜色看成点,把一个珠子看成一个无向边,则问题有解,当且仅当图中存在欧拉回路.于是先判断由题意构建出来

UVa 10054 The Necklace【欧拉回路】

题意:给出n个珠子,珠子颜色分为两半,分别用1到50之间的数字表示, 现在给出n个珠子分别的颜色,问是否能够串成一个环.即为首尾相连,成为一个回路 判断是否构成一个环,即判断是否为欧拉回路,只需要判断度数是不是偶数就可以了 (这道题目给出的珠子是在一个连通块上的,所以不用考虑连通) 然后输出结果要逆序输出,见这一篇,非常的详细 http://www.cnblogs.com/scau20110726/archive/2012/11/09/2762371.html 1 #include<iostre

【欧拉回路】UVA - 10054 The Necklace

题目大意: 一个环被切割成了n个小块,每个小块有头尾两个关键字,表示颜色. 目标是判断给出的n个小块能否重构成环,能则输出一种可行解(按重构次序输出n个色块的头尾颜色).反之输出"some beads may be lost". 解题思路: 一开始想的曼哈顿回路,WA了.后来依靠别人的智慧,知道正解是欧拉回路. 在知道这道题是欧拉回路的情况下就变得很简单了,就是一道模板题--每种颜色看成一个点,每个小块代表两点之间连接的边,如果存在欧拉回路就有可行解. 不存在欧拉回路有两种情况:1.图

UVA 10054 The Necklace

题意: 项链散了  每个珠子前端后端分别有颜色  在项链中  相邻的珠子的相邻的那一端颜色相同  问  找到的珠子能不能重新串起一根项链 思路: 比较经典的欧拉回路题  Fleury算法解决问题 代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define M 60 int n,ans,top,m,t,T; int Edge[M][M],path[M*M],

uva 10054 The Necklace(欧拉通路)

提交了7次,总算AC了.题目不难,就是判断下欧拉通路.注意细节. /* Status:AC Title :The Necklace */ #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <queue> #include <s

The Necklace UVA - 10054 (无向图的欧拉回路)

The Necklace UVA - 10054 题意:每个珠子有两个颜色,给n个珠子,问能不能连成一个项链,使得项链相邻的珠子颜色相同. 把颜色看做点,珠子内部连一条边,无向图求欧拉回路. 这里我用的并查集. 输出路径就dfs就行了 1 #include <bits/stdc++.h> 2 using namespace std; 3 int g[55][55]; 4 int f[55]; 5 int deg[55]; 6 int n; 7 8 int gf(int x) 9 { 10 re

UVA 11054 The Necklace 转化成欧拉回路

题意比较简单,给你n个项链碎片,每个碎片的两半各有一种颜色,最后要把这n个碎片串成一个项链,要求就是相邻碎片必须是同种颜色挨着. 看了下碎片总共有1000个,颜色有50种,瞬间觉得普通方法是无法在可控时间内做出来的,因为碎片到底放哪里以及是正着放还是反着放都是不可控的. 这个时候数学建模就真的好重要了,如果我们能把颜色作为节点,一个碎片就表示两个节点连了一条路,那其实就是走了一遍欧拉回路,就意味着项链做成了. 太叼了,这个思想真心不错...LRJ书上的提示,否则我还真是想不到可以这样. 不过还有