首页 > 其他 > 详细

[2016-01-27][POJ][2230][Watchcow]

时间:2016-01-27 23:17:36      阅读:560      评论:0      收藏:0      [点我收藏+]
[2016-01-27][POJ][2230][Watchcow]

H - 欧拉图 中级者向
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

Hint

OUTPUT DETAILS: 

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

  • 时间:2016-01-26 21:58:37 星期二
  • 题目编号:POJ 2230 watchcow
  • 题目大意:给出n个顶点,n条边,以1为起点,输出 经过每条边两次(每次方向相反),的路径
  • 分析:
    • 无向边 相当于 两个方向方向相反的有向边
  • 方法:
    • DFS,从1开始dfs,遍历每一个顶点,每走过一条边,打上标记,直到无路可走
  • 解题过程遇到问题:
    •  题目是无向边,最大边数是 2*maxm
    • 邻接表的数组实现.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define getint(x) int (x);scanf("%d",&(x))
#define get2int(x,y) int (x),(y);scanf("%d%d",&(x),&(y))
#define get3int(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z))
#define getll(x) LL (x);scanf("%I64d",&(x))
#define get2ll(x,y) LL (x),(y);scanf("%I64d%I64d",&(x),&(y))
#define get3ll(x,y,z) LL (x),(y),(z);scanf("%I64d%I64d%I64d",&(x),&(y),&(z))
#define getdb(x) double (x);scanf("%lf",&(x))
#define get2db(x,y) double (x),(y);scanf("%lf%lf",&(x),&(y))
#define get3db(x,y,z) double (x),(y),(z);scanf("%lf%lf%lf",&(x),&(y),&(z))
 
#define getint2(x) scanf("%d",&(x))
#define get2int2(x,y) scanf("%d%d",&(x),&(y))
#define get3int2(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define getll2(x) scanf("%I64d",&(x))
#define get2ll2(x,y) scanf("%I64d%I64d",&(x),&(y))
#define get3ll2(x,y,z) scanf("%I64d%I64d%I64d",&(x),&(y),&(z))
#define getdb2(x) scanf("%lf",&(x))
#define get2db2(x,y) scanf("%lf%lf",&(x),&(y))
#define get3db2(x,y,z) scanf("%lf%lf%lf",&(x),&(y),&(z))
 
#define getstr(str) scanf("%s",str)
#define get2str(str1,str2) scanf("%s",str1,str2)
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)
#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)
 
const int maxn = 10000 + 100;
const int maxm = (50000 + 100)*2;
int head[maxn];//head[i]表示第i个顶点,第一个链节的编号
int next[maxm];//表示 第 q 号链节,下一个链节的编号
int end[maxm];//表示 第 q 个链节 的终点
int vis[maxm];//表示第q个链节是否被访问过.
void addedge(int from,int to){
        static int q = 1;
        end[q] = to;
        next[q] = head[from];
        head[from] = q++;
}
 
 
int n,m;
 
void dfs(int cur){
        for(int q = head[cur];q;q = next[q])
        {
                if(!vis[q])
                {
                        vis[q] = 1;
                        dfs(end[q]);
                }
        }
        printf("%d\n",cur);
}
 
int main()
{
        get2int2(n,m);
        int a,b;
        //CLR(next,0);CLR(end,0);CLR(head,0);CLR(vis,0);
        FOR(i,0,m){
                get2int2(a,b);
                addedge(a,b);
                addedge(b,a);
        }
        dfs(1);
        return 0;
}






[2016-01-27][POJ][2230][Watchcow]

原文:http://www.cnblogs.com/qhy285571052/p/5164724.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!