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

 
解题:欧拉回路
 
 #include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[];
int head[maxn],tot,n,m;
bool vis[];
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void dfs(int u) {
for(int &i = head[u]; ~i; i = e[i].next) {
if(vis[i]) continue;
vis[i] = true;
dfs(e[i].to);
}
printf("%d\n",u);
}
int main() {
int u,v;
while(~scanf("%d%d",&n,&m)) {
memset(head,-,sizeof head);
memset(vis,false,sizeof vis);
for(int i = tot = ; i < m; ++i) {
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs();
}
return ;
}

POJ 2230 Watchcow的更多相关文章

  1. [欧拉] poj 2230 Watchcow

    主题链接: http://poj.org/problem? id=2230 Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

  2. POJ 2230 Watchcow(欧拉回路:输出点路径)

    题目链接:http://poj.org/problem?id=2230 题目大意:给你n个点m条边,Bessie希望能走过每条边两次,且两次的方向相反,让你输出以点的形式输出路径. 解题思路:其实就是 ...

  3. POJ 2230 Watchcow(有向图欧拉回路)

    Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the ...

  4. POJ 2230 Watchcow (欧拉回路)

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5258   Accepted: 2206   Specia ...

  5. POJ 2230 Watchcow && USACO Watchcow 2005 January Silver (欧拉回路)

    Description Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to wal ...

  6. POJ 2230 Watchcow 【欧拉路】

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6336   Accepted: 2743   Specia ...

  7. POJ 2230 Watchcow 欧拉图

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8800   Accepted: 3832   Specia ...

  8. POJ 2230 Watchcow 欧拉回路的DFS解法(模板题)

    Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9974 Accepted: 4307 Special Judg ...

  9. poj 2230 Watchcow(欧拉回路)

    关键是每条边必须走两遍,重复建边即可,因为确定了必然存在 Euler Circuit ,所以所有判断条件都不需要了. 注意:我是2500ms跑过的,鉴于这道题ac的code奇短,速度奇快,考虑解法应该 ...

随机推荐

  1. POJ 2137

    水,dp[i][j][k],设为当前为i个牛,在它喜欢的j个位置,而第一个牛在k个位置,很明显了,其实不过是枚举. #include <iostream> #include <cst ...

  2. SDWebImage源代码解析(二)

    上一篇:SDWebImage源代码解析(一) 2.缓存 为了降低网络流量的消耗.我们都希望下载下来的图片缓存到本地.下次再去获取同一张图片时.能够直接从本地获取,而不再从远程server获取.这样做的 ...

  3. [React] Reference a node using createRef() in React 16.3

    In this lesson, we look at where we came from with refs in React. Starting with the deprecated strin ...

  4. uva 10061(数学)

    题解:题目要在b进制下输出的是一个数字阶乘后有多少个零,然后输出一共同拥有多少位.首先计算位数,log(n)/log(b) + 1就是n在b进制下有多少位,而log有个公式就是log(M×N) = l ...

  5. Android 获取屏幕截图 和保存到本地的sd卡路径下

    /** * 获取和保存当前屏幕的截图 */ private void GetandSaveCurrentImage() { //1.构建Bitmap WindowManager windowManag ...

  6. uva 10534 Wavio Sequence LIS

    // uva 10534 Wavio Sequence // // 能够将题目转化为经典的LIS. // 从左往右LIS记作d[i],从右往左LIS记作p[i]; // 则最后当中的min(d[i], ...

  7. C-结构体应用(10)

    结构体是用来定义多种类型的复合类型,在 C语言中与类的区别在于结构体注重的是数据而类除了数据还包含函数,第2点区别在于结构体所声明的成员默认是"public"点.而类的默认是pri ...

  8. Apache支持多端口配置处理

    玩了这么多年,终于知道原来Apache也可以支持多个端口监听!!!!!!!!!!!!!!!!!!!这样就可以一个服务器,存放多个项目了,一个ip多个端口,匹配多个项目. 试了下linux,也是可以的 ...

  9. Swift - 制作一个在线流媒体音乐播放器(使用StreamingKit库)

    在之前的文章中,我介绍了如何使用 AVPlayer 制作一个简单的音乐播放器(点击查看1.点击查看2).虽然这个播放器也可以播放网络音频,但其实际上是将音频文件下载到本地后再播放的. 本文演示如何使用 ...

  10. 蓝桥杯--算法提高 排列数 (简单dfs)

    算法提高 排列数   时间限制:1.0s   内存限制:256.0MB      问题描述 0.1.2三个数字的全排列有六种,按照字母序排列如下: 012.021.102.120.201.210 输入 ...