[POJ | Nowcoder] Watchcow | 欧拉回路 点路径输出

Posted PushyTao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ | Nowcoder] Watchcow | 欧拉回路 点路径输出相关的知识,希望对你有一定的参考价值。

Description

Bessie’s been appointed the new watch-cow for the farm. Every night, it’s her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, and then returns to the barn when she’s done.

If she were a more observant cow, she might be able to just walk each of M ( 1 < = M < = 50 , 000 ) (1 <= M <= 50,000) (1<=M<=50,000) bidirectional trails numbered 1…M between N ( 2 < = N < = 10 , 000 ) (2 <= N <= 10,000) (2<=N<=10,000) fields numbered 1…N on the farm once and be confident that she’s seen everything she needs to see. But since she isn’t, she wants to make sure she walks down each trail exactly twice. It’s also important that her two trips along each trail be in opposite directions, so that she doesn’t miss the same thing twice.

A pair of fields might be connected by more than one trail. Find a path that Bessie can follow which will meet her requirements. Such a path is guaranteed to exist.
Input

  • Line 1: Two integers, N and M.

  • Lines 2…M+1: Two integers denoting a pair of fields connected by a path.
    Output

  • Lines 1…2M+1: A list of fields she passes through, one per line, beginning and ending with the barn at field 1. If more than one solution is possible, output any solution.
    Sample Input

4 5
1 2
1 4
2 3
2 4
3 4

Sample Output

1
2
3
4
2
1
4
3
2
4
1

Hint

OUTPUT DETAILS:

Bessie starts at 1 (barn), goes to 2, then 3, etc…
Source

USACO 2005 January Silver

欧拉回路的路径输出,记得要再遍历完所有的点之后在输出这个点,否则不正确

int n,m;
struct node{
	int u,v,nex;
	bool vis;
}e[maxn];
int cnt,head[maxn];
void _Init(){
	cnt = 0;
	for(int i=1;i<=n;i++) head[i] = -1;
}
void add(int u,int v){
	e[cnt].u = u;
	e[cnt].v = v;
	e[cnt].nex = head[u];
	head[u] = cnt ++;
	
	e[cnt].u = v;
	e[cnt].v = u;
	e[cnt].nex = head[v];
	head[v] = cnt ++;
}
deque<int> vet;
void dfs(int to){
	for(int i=head[to];~i;i=e[i].nex){
		int to = e[i].v;
		if(e[i].vis) continue;
		e[i].vis = true;
		dfs(to);
	}
	cout << to << endl;/// 
}
int main() {
	n = read,m = read;
	_Init();
	for(int i=1;i<=m;i++){
		int u = read,v = read;
		add(u,v);
	}
	dfs(1);
	return 0;
}

以上是关于[POJ | Nowcoder] Watchcow | 欧拉回路 点路径输出的主要内容,如果未能解决你的问题,请参考以下文章

POJ2230 Watchcow

POJ 2230 Watchcow

POJ 2230 Watchcow

POJ2230Watchcow

poj2230 Watchcow(欧拉回路)

POJ2230 Watchcow - 欧拉回路