[POJ] John‘s trip | 欧拉回路 | 边序列字典序最小 + 建图

Posted PushyTao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ] John‘s trip | 欧拉回路 | 边序列字典序最小 + 建图相关的知识,希望对你有一定的参考价值。

网址链接

Description

Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible. Very soon he realized that the best way to do it was to travel through each street of town only once. Naturally, he wanted to finish his trip at the same place he started, at his parents’ house.

The streets in Johnny’s town were named by integer numbers from 1 to n, n < 1995 n < 1995 n<1995. The junctions were independently named by integer numbers from 1 to m, m < = 44 m <= 44 m<=44. No junction connects more than 44 streets. All junctions in the town had different numbers. Each street was connecting exactly two junctions. No two streets in the town had the same number. He immediately started to plan his round trip. If there was more than one such round trip, he would have chosen the one which, when written down as a sequence of street numbers is lexicographically the smallest. But Johnny was not able to find even one such round trip.

Help Johnny and write a program which finds the desired shortest round trip. If the round trip does not exist the program should write a message. Assume that Johnny lives at the junction ending the street appears first in the input with smaller number. All streets in the town are two way. There exists a way from each street to another street in the town. The streets in the town are very narrow and there is no possibility to turn back the car once he is in the street
Input

Input file consists of several blocks. Each block describes one town. Each line in the block contains three integers x; y; z, where x > 0 and y > 0 are the numbers of junctions which are connected by the street number z. The end of the block is marked by the line containing x = y = 0. At the end of the input file there is an empty block, x = y = 0.
Output

Output one line of each block contains the sequence of street numbers (single members of the sequence are separated by space) describing Johnny’s round trip. If the round trip cannot be found the corresponding output block contains the message “Round trip does not exist.”
Sample Input

1 2 1
2 3 2
3 1 6
1 2 5
2 3 3
3 1 4
0 0
1 2 1
2 3 2
1 3 3
2 4 4
0 0
0 0

Sample Output

1 2 3 5 4 6 
Round trip does not exist.

题意:
给出m个点m<=44,n条边,n<1995的图,问是否有欧拉回路{
1. 有 则输出字典序最小的那条
2. 无 则输出"Round trip does not exist."
}

欧拉回路基本概念+判断+求解
差点把图建岔劈了
总结

因为在输入的过程中,边的数量和点的数量都不是确定的,所以说我们必须要在输入的过程中记录点的数量以及边的数量,因为点的编号以及边的编号都是连续的,所以说我们可以直接记录点的编号的最大值以及边的编号最大值即可
假如统计出来的边的的最大编号为max_edge,那么说边的最大编号为max_edge,我们从1号点开始,从 1 1 1 m a x _ e d g e max\\_edge max_edge便利这个点连向的所有边,如果当前点联想的边没有走过,就标记这条边,然后继续以这条边的另一端为起点继续深搜,中间过程中记录答案,因为每条边最只会被访问一次,所以说复杂度不会很高

ac_code:

int n;
int cnt,head[50];
int deg[50];
int gra[50][2007];
int u,v,id;
int start,max_edge;
bool vis[2007];
void _Init(){
	for(int i=1;i<=2000;i++) vis[i] = false,deg[i] = 0;
	memset(gra,0,sizeof gra);
}
vector<int> ans;
void dfs(int u){
	for(int i=1;i<=max_edge;i++){
		if(!vis[i] && gra[u][i]){
			vis[i] = 1;
			dfs(gra[u][i]);
			ans.push_back(i);
		}
	}
}
int main() {
	while(true){
		start = 0x3f3f3f3f,max_edge = 0, n = 0;
		ans.clear();
		_Init();
		u = read,v =read;
		if(u == 0 && v == 0) break;
		id = read;
		start = min(u, v);
		n = max(u, v);
		max_edge = max(max_edge,id);
		gra[u][id] = v;
		gra[v][id] = u;
		deg[u] ++;
		deg[v] ++;
		while(true){
			u = read,v = read;
			if(!(u + v)) break;
			id = read;
			start = min(start,min(u,v));
			n = max(n,max(u, v));
			max_edge = max(max_edge, id);
			gra[u][id] = v;
			gra[v][id] = u;
			deg[u] ++;
			deg[v] ++;
		}
		int flag = 0;
//		debug(n);
//		debug(max_edge);
//		debug(start);
		for(int i=1;i<=n;i++){
			if(deg[i] % 2) {
				puts("Round trip does not exist.");
				flag = 1;
				break;
			}
		}
		if(flag) continue;
		assert(start == 1);
		dfs(start);
		for(int i=1;i<=max_edge;i++){
			printf("%d%c",ans.back(),(i!=max_edge?' ':'\\n'));
			ans.pop_back();
		}
	}
	return 0;
}
/**
1 2 1
2 3 2
3 1 6
1 2 5
2 3 3
3 1 4
0 0
1 2 1
2 3 2
1 3 3
2 4 4
0 0
0 0

**/

以上是关于[POJ] John‘s trip | 欧拉回路 | 边序列字典序最小 + 建图的主要内容,如果未能解决你的问题,请参考以下文章

John's Trip

hdu 3018 Ant Trip 欧拉回路+并查集

HDU-3018-Ant Trip-欧拉回路+并查集

Ant Trip(欧拉回路+并查集)

HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

poj1637 Sightseeing tour[最大流+欧拉回路]