poj 2230 Watchcow(欧拉回路)
来源:互联网 发布:商品条码数据库下载 编辑:程序博客网 时间:2024/04/24 21:57
题目链接
Watchcow
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 7537 Accepted: 3305 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.
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.
* 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 51 21 42 32 43 4
Sample Output
12342143241
Hint
OUTPUT DETAILS:
Bessie starts at 1 (barn), goes to 2, then 3, etc...
Bessie starts at 1 (barn), goes to 2, then 3, etc...
Source
USACO 2005 January Silver
题意:
给你一个无向图,要求找一条路径,走过每一条边恰好两次,且每次走的方向不同。很容易就能想到把这图转化为有向图求欧拉回路。题目保证一定能找到从1点出发回到1点的答案。
有了前面几题的基础,做这一题就很容易了。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<set>using namespace std;const int MAXN=10000+100;vector<int> g[MAXN];void dfs(int u){while(g[u].size()){int v=g[u][0];g[u].erase(g[u].begin());vector<int>::iterator it=g[v].begin();for(;it!=g[v].end();++it){if(*it==u) break;}g[v].erase(it);dfs(v);}printf("%d\n",u);}int main(){int n,m;scanf("%d%d",&n,&m);while(m--){int u,v;scanf("%d%d",&u,&v);g[u].push_back(v);g[v].push_back(u);g[u].push_back(v);g[v].push_back(u);}dfs(1);return 0;}
0 0
- POJ 2230 Watchcow 【欧拉回路】
- poj 2230 Watchcow (DFS + 欧拉回路)
- poj 2230 Watchcow(欧拉回路)
- poj - 2230 - Watchcow(欧拉回路)
- POJ 2230 Watchcow 欧拉回路题解
- [欧拉回路] poj 2230 Watchcow
- POJ 2230 Watchcow(欧拉回路)
- POJ 2230 Watchcow (欧拉回路)
- poj 2230 Watchcow 欧拉回路
- POJ 2230 Watchcow (欧拉回路)
- POJ 2230 Watchcow( 欧拉回路 )
- POJ 2230 Watchcow (欧拉回路)
- Poj 2230 Watchcow【欧拉回路】
- poj 2230 Watchcow(欧拉回路)
- POJ 2230 Watchcow 欧拉回路
- POJ 2230 Watchcow(欧拉回路+DFS)
- POJ 2230 - Watchcow 输出欧拉回路点的路径...
- POJ 2230 Watchcow(欧拉回路:输出点轨迹)
- 键盘输入10个数,放到数组中去除该数组中大于10的数,将该数组中的数字写入到本地文件
- HTTP3xx重定向
- python caffe training solve.py
- C语言 打印菱形(可变行数)
- MySQL中函数CONCAT及GROUP_CONCAT
- poj 2230 Watchcow(欧拉回路)
- 后缀数组代码理解加注释
- 算法分析与设计-动态规划0
- C++利用socket的客户端和服务器之间传输文件
- Spring Data JPA使用JpaRepository自动装配报No qualifying bean of type错误
- 51nod 1065 最小正子段和(前缀和)
- C++ hdu 2026 首字母大写
- 判断一棵二叉树是否是另一棵二叉树的子树
- 【NOIP 模拟题】[T1]return(模拟)