POJ 2230 Watchcow

Watchcow

Time Limit: 3000ms

Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2230
64-bit integer IO format: %lld      Java class name: Main

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...

Source

USACO 2005 January Silver

解题:欧拉回路

 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 const int maxn = 10010;
 5 struct arc {
 6     int to,next;
 7     arc(int x = 0,int y = -1) {
 8         to = x;
 9         next = y;
10     }
11 } e[500000];
12 int head[maxn],tot,n,m;
13 bool vis[500000];
14 void add(int u,int v) {
15     e[tot] = arc(v,head[u]);
16     head[u] = tot++;
17 }
18 void dfs(int u) {
19     for(int &i = head[u]; ~i; i = e[i].next) {
20         if(vis[i]) continue;
21         vis[i] = true;
22         dfs(e[i].to);
23     }
24     printf("%d\n",u);
25 }
26 int main() {
27     int u,v;
28     while(~scanf("%d%d",&n,&m)) {
29         memset(head,-1,sizeof head);
30         memset(vis,false,sizeof vis);
31         for(int i = tot = 0; i < m; ++i) {
32             scanf("%d%d",&u,&v);
33             add(u,v);
34             add(v,u);
35         }
36         dfs(1);
37     }
38     return 0;
39 }

时间: 2024-08-03 09:13:07

POJ 2230 Watchcow的相关文章

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

H - 欧拉图 中级者向 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2230 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 s

POJ 2230 Watchcow 欧拉回路题解

本题就是以每个节点和节点之间建路,而且说明是无向图,不过这里有个技巧,就是根据题意把它当成有向图来做,就成了直接查找有向图的欧拉回路就可以了.因为题意是需要每条边都走两遍的,而且每次走的方向相反. 观察出这点,那么这道题就好做啦,直接建图,Feury递归求解就可以了. 建图注意需要建邻接表,不要建矩阵,因为建成矩阵,那么会很大很大,而根据题意,建成邻接表最多只需要5倍的顶点数. 打印的顺序是逆过来打和顺着打都可以的,因为先走那边都可以. #include <stdio.h> #include

[欧拉回路] poj 2230 Watchcow

题目链接: http://poj.org/problem?id=2230 Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6055   Accepted: 2610   Special Judge Description Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk acr

POJ 2230 Watchcow &amp;&amp; USACO Watchcow 2005 January Silver (欧拉回路)

题意: Bessie 最近做了农场看守,他每天晚上的工作就是巡视农场并且保证没有坏人破坏农场.从谷仓出发去巡视,并且最终回到谷仓. Bessie 视力不是很好,不能像其他农场的看守一样,对农场的每一条连接不同场地的路走一遍就可以发现是不是有异常情况,他需要每条路都走两遍,并且这两边必须是不同的方向,因为他觉得自己应该不会两次都忽略农场中的异常情况. 每块地之间一定会由至少一条路相连.现在的任务就是帮他制定巡视路线.前提假设一定存在满足题意的路径. 输入: 第一行输入两个数N(2 <= N <=

POJ 2230 Watchcow (欧拉回路)

题目地址:POJ 2230 最普通的欧拉回路.边不重复记录点.不多说. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.

POJ 2230 解题报告

分析: 基础的欧拉路算法,变化在于要求每条边正向和反向各走一遍. 链式前向星构图,只要标记走过的单向边,边找边输出即可. code #include <iostream> #include <cstdio> using namespace std; struct node { int v, ne; } edge[100009]; int head[10009], vis[100009], cnt = 1; int n, m, x, y; void addedge (int u, i

欧拉回路求路径POJ 2230

Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8841   Accepted: 3854   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 ev

(有向图欧拉回路求解) poj 2230

Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6255   Accepted: 2708   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 ev

欧拉回路判定与求解

图G是欧拉图,即存在欧拉回路的条件::smile: 1.图是联通的 2.对于无向图,奇度数点个数为0.对于有向图,每个顶点出度等于入度. 欧拉回路算法模板(链式前向星和DFS实现): int ans[N]; int k = 0; int vis[2*M]; void DFS(int now) { for(int u=first[now];u!=-1;u=G[u].next) { if(!vis[u]) { vis[u] = 1; //标记当前边 vis[u^1] = 1; //标记反向的另一条边