poj 2230 Watchcow(欧拉回路)

来源:互联网 发布:商品条码数据库下载 编辑:程序博客网 时间:2024/04/24 21:57

题目链接
Watchcow
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 7537 Accepted: 3305 Special Judge

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) bidirectional trails numbered 1..M between N (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 51 21 42 32 43 4

Sample Output

12342143241

Hint

OUTPUT DETAILS: 

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

Source

USACO 2005 January Silver


题意:

给你一个无向图,要求找一条路径,走过每一条边恰好两次,且每次走的方向不同。很容易就能想到把这图转化为有向图求欧拉回路。题目保证一定能找到从1点出发回到1点的答案。


有了前面几题的基础,做这一题就很容易了。


#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<set>using namespace std;const int MAXN=10000+100;vector<int> g[MAXN];void dfs(int u){while(g[u].size()){int v=g[u][0];g[u].erase(g[u].begin());vector<int>::iterator it=g[v].begin();for(;it!=g[v].end();++it){if(*it==u) break;}g[v].erase(it);dfs(v);}printf("%d\n",u);}int main(){int n,m;scanf("%d%d",&n,&m);while(m--){int u,v;scanf("%d%d",&u,&v);g[u].push_back(v);g[v].push_back(u);g[u].push_back(v);g[v].push_back(u);}dfs(1);return 0;}

0 0