UVA - 10054 The Necklace (欧拉回路)

  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



题目意思:
有一堆散落的项链的的珠子,珠子有可能重复的出现,问我们能否连接成一条项链,要求该项链的每一节的两个珠子要满足,第一个珠子必须和前一节的第二个珠子相同,对于最后一节的第二个珠子必须和第一节的第一个相同。(即能构成一个环)

问能否实现,如果可以输出其中一种路径

欧拉回路:
如果连通图中顶点的入度全部为偶数则欧拉回路存在


//无重复边的欧拉回路
void euler(int u) {
	for(int v = 0; v < n; v++) {
		if(G[u][v] && !vis[u][v]) {
			vis[u][v] = vis[v][u] = 1;
			eulur(v);
			printf("%d %d\n",v,u);
		}
	}
}
//有重复边的欧拉回路
void euler(int u) {
	for(int v = 0; v < n; v++) {
		if(G[u][v]) {
			G[u][v]--;
			G[v][u]--;
			eulur(v);
			printf("%d %d\n",v,u);
		}
	}
}


#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int N = 55;
int edge[N][N];
int deg[N];

void  init() {
	memset(edge,0,sizeof(edge));
	memset(deg,0,sizeof(deg));
}

bool judge() {
	for(int i = 0; i < N; i++) {
		if(deg[i]%2 == 1) {
			return false;
		}
	}
	return true;
}

void euler(int u) {
	for(int v = 0; v < N; v++) {
		if(edge[u][v]) {
			edge[u][v]--;
			edge[v][u]--;	
			euler(v);
			printf("%d %d\n",v,u);
		}
	}
}
int main() {
	int T;
	int cas = 0;
	int u,v;
	int n;
	while( scanf("%d",&T) != EOF) {
		while(T--) {
			init();
			scanf("%d",&n);
			for(int i = 0; i < n; i++){	
				scanf("%d%d",&u,&v);
				edge[u][v]++;
				edge[v][u]++;
				deg[u]++;
				deg[v]++;
			}
			if(cas) {
				printf("\n");
			}
			printf("Case #%d\n",++cas);
			if( judge() ) {
				for(int i = 0; i < N; i++) {
					if(deg[i]) {
						euler(i);
					}
				}
			}else {
				printf("some beads may be lost\n");
			}
		}
	}
	return 0;
}

你可能感兴趣的:(uva,the,Necklace)