POJ 2230 Watchcow

Watchcow
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5178   Accepted: 2171   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 5
1 2
1 4
2 3
2 4
3 4

Sample Output

1
2
3
4
2
1
4
3
2
4
1

欧拉回路问题,个人认为是稍微变过型的欧拉回路
采用链式前向星存储图,注意由于是无向图,所以每条边要存储两次
搜索欧拉回路采用了DFS,基本上就是套模板

 1 #include<iostream>
 2 #include<cstring>
 3 
 4 using namespace std;
 5 
 6 typedef struct
 7 {
 8     long to;
 9     long next;
10 } EDGE;
11 
12 long tot=0;
13 bool visited[100000];
14 long head[10001],ans[100000];
15 EDGE e[100000];
16 
17 void dfs(long k)
18 {
19     for(int i=head[k];i!=-1;i=e[i].next)
20     {
21         if(!visited[i])
22         {
23             visited[i]=true;
24             dfs(e[i].to);
25             ans[tot++]=e[i].to;
26         }
27     }
28 }
29 
30 int main()
31 {
32     int n;
33     long m;
34 
35     cin>>n>>m;
36 
37     memset(head,-1,sizeof(head));
38     memset(visited,false,sizeof(visited));
39     ans[0]=1;
40 
41     for(long i=0,temp_1,temp_2,t=2*m;i<t;i+=2)
42     {
43         cin>>temp_1>>temp_2;
44         e[i].to=temp_2;
45         e[i].next=head[temp_1];
46         head[temp_1]=i;
47         e[i+1].to=temp_1;
48         e[i+1].next=head[temp_2];
49         head[temp_2]=i+1;
50     }
51 
52     dfs(1);
53     ans[tot]=1;
54 
55     for(int i=0;i<=tot;i++)
56         cout<<ans[i]<<endl;
57 
58     return 0;
59 }
[C++]

 

posted @ 2013-05-26 22:36  ~~Snail~~  阅读(162)  评论(0编辑  收藏  举报