您的位置:首页 > 其它

Uva 10054 - The Necklace(欧拉回路)

2013-04-11 11:00 357 查看
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

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define ALNUM 51
#define MAXN 10010
int word[ALNUM+4][ALNUM+4];
int times[ALNUM+4];
int record[ALNUM+4];
int print[MAXN][2];
int n, sum;

int Traverse(int x)
{//输出路径
int j;
for(j=0; j<ALNUM; ++j)
{
if(word[x][j] != 0)
{
word[x][j]--;
word[j][x]--;
Traverse(j);
printf("%d %d\n", j, x);
}
}

}

int TraverseOut(int current)
{//DFS判断图是否连通
int j;
times[current] = 1;
for(j=0; j<ALNUM; ++j)
{
if(word[current][j] != 0 && !times[j])
TraverseOut(j);
}
return 0;
}

int main()
{
int T, i, j, len, flag, point, list, t = 0, cnt, count, k, x, y;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
getchar();
memset(word, 0, sizeof(word));
memset(record, 0, sizeof(record));
for(i=0; i<n; ++i)
{
scanf("%d%d", &point, &list);
++word[point][list];
++word[list][point];
record[point]++;
record[list]++;
}
flag = 0;
for(i=0; i<ALNUM; ++i)
{//欧拉回路形成的条件之一,判断结点度数是否为偶
if(record[i]%2 != 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
for(i=0; i<ALNUM; ++i)
if(record[i] != 0)
{
memset(times, 0, sizeof(times));
TraverseOut(i);
for(j=0; j<ALNUM; ++j)
{
if(record[j] != 0 && !times[j])
{
flag = 1;
break;
}
}
break;
}
}
if(t != 0) printf("\n");
printf("Case #%d\n", ++t);
if(flag == 0)
{
for(i=0; i<ALNUM; ++i)
if(record[i] != 0)
{
Traverse(i);
break;
}
}
else printf("some beads may be lost\n");
}
return 0;
}


解题思路:

经过图中每条边一次且仅一次并且行遍图中每个顶点的通路(回路),称为欧拉通路或欧拉迹(欧拉回路或欧拉闭迹)存在欧拉回路的图称为欧拉图

对于无向图具有欧拉回路,当且仅当该图是连通图且无奇数顶点。无向图G具有欧拉通路、但无欧拉回路,当且仅当G是连通图且恰好有两个奇度顶点,这两个奇度顶点是每条欧拉通路的端点

有向图D具有欧拉回路,当且仅当D是连通的且每个顶点的入度等于出度。有向图D具有欧拉通路、但无欧拉回路,当且仅当D是连通的,且除了两个顶点外,其余顶点的入度均等于出度。这两个特殊的顶点中,一个顶点的入度比出度大1,另一个顶点的入度比出度小1,此时,任何欧拉通路以前一个顶点为终点,以后一个顶点为始点

Ps:下次应该要记得将WA的版本的代码保存下来,这样才能进行比较思路的转换过程,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: