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

 
思路:打印欧拉通路,题目保证有解,直接DFS打印即可,代码如下:
const int maxm = ;
const int maxn = ; struct Node {
int from, to;
Node(int _from, int _to) : from(_from), to(_to){}
}; int N, M, vis[maxn*];
vector<int> ans, G[maxm];
vector<Node> edges; void addedge(int u,int v) {
edges.push_back(Node(u, v));
G[u].push_back(edges.size() - );
} void dfs(int x) {
int len = G[x].size();
for(int i = ; i < len; ++i) {
if(!vis[G[x][i]]) {
vis[G[x][i]] = ;
dfs(edges[G[x][i]].to);
ans.push_back(edges[G[x][i]].to);
}
}
} int main() {
scanf("%d%d", &N, &M);
for (int i = ; i < M; ++i) {
int t1, t2;
scanf("%d%d", &t1, &t2);
addedge(t1, t2);
addedge(t2, t1);
}
dfs();
int len = ans.size();
for(int i = ; i < len; ++i)
printf("%d\n", ans[i]);
printf("1\n");
return ;
}

Day4 - D - Watchcow POJ - 2230的更多相关文章

  1. 欧拉回路输出(DFS,不用回溯!)Watchcow POJ 2230

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8109   Accepted: 3551   Special Judge D ...

  2. [欧拉] poj 2230 Watchcow

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

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

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

  4. 【POJ 2230】 Watchcow

    [题目链接] http://poj.org/problem?id=2230 [算法] 欧拉回路 [代码] #include <algorithm> #include <bitset& ...

  5. POJ 2230 Watchcow

    Watchcow Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2 ...

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

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

  7. POJ 2230 Watchcow (欧拉回路)

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

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

  9. POJ 2230 Watchcow 【欧拉路】

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

随机推荐

  1. 3 JavaScript正则表达式

    正则表达式:Regular(有规则的) Expression 正则表达式是由一个字符序列形成的搜索模式,可用于文本搜索和文本替换 常见于字符串的search和replace方法 var str = & ...

  2. 洛谷P1198 [JSOI2008]最大数(线段树)

    题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:LLL不超过当前数列的长度.(L> ...

  3. tensorflow变量的使用(02-2)

    import tensorflow as tf x=tf.Variable([1,2]) a=tf.constant([3,3]) sub=tf.subtract(x,a) #增加一个减法op add ...

  4. 第1节 Scala基础语法:11、映射;12、元组

    5.2.   映射 在Scala中,把哈希表这种数据结构叫做映射. 1.1.1.    构建映射 (1)构建映射格式 1.val map=Map(键 -> 值,键 -> 值....) 2. ...

  5. 实用类-<Math类常用>

    Math.random() //取0~1之间的随机数(不包括1) Math.max(数字1,数字2) //取两个数中最大的一个 Math.min(数字1,数字2) //取两个数中最小的一个 Math. ...

  6. 解题报告:luogu P2220

    指挥使走后一脸懵逼,然后想起了一道水\(SB\)的省选题. 这是毒瘤乘法分配率的应用,似乎还有一篇,算是入门题. 对了,这题连接:P2220 [HAOI2012]容易题 然而蒟蒻还是先自闭了一会... ...

  7. CSP-J2019 纪念品

    Description: Solution: 第一天买入,第二天卖出,在干些别的,再把第二天刚卖出的再买回来,就相当于是啥也没干.也就是说手中的物品本身要算在手中的钱中.这也就是为什么 dp 的状态可 ...

  8. java swing简介

    java应用程序用户界面开发包 Swing是一个用于开发Java应用程序用户界面的开发工具包.它以抽象窗口工具包(AWT)为基础使跨平台应用程序可以使用任何可插拔的外观风格.Swing开发人员只用很少 ...

  9. Js为Dom元素绑定事件须知

    为异步加载的Dom 元素绑定事件必须在加载完成之后绑定: $('body').load('LearnClickBinding.ashx');$('a').click(function () { ale ...

  10. C#对象、List<>转DataTable

    public static DataTable ObjectToTable(object obj)         {             try {                 Type t ...