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 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
分析:
题目上要求的是从1号点出发,走过一个回路之后再回到1号点,但是要求的是同一条路径要按照相反的方向各走一遍,到这里我们必须理解到一点就是,对于图上的点来所,有且仅有一个点要走3次,其余的点都要走两次。
由于是无向边,而且每条边要求正反各走一次,所以一定存在欧拉回路。存图时把每条无向边看成两条相反的有向边,直接利用欧拉回路求解。
但是这样的路径走法可能有许多种,我们只需要输出其中一种即可。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m;
struct Node
{
int next ;
int to;
} node[100005];
int head[20009];
int Count=0;
void addEdg(int u,int v)//图正反方向都要存储一遍
{
Count++;
node[Count].to=v;
node[Count].next=head[u];
head[u]=Count;
Count++;
node[Count].to=u;
node[Count].next=head[v];
head[v]=Count;
}
bool vis[20009];
void dfs(int u)
{
for(int i=head[u]; i ; i=node[i].next)
{
if(vis[i]==1)continue;//该边已经走过了,就不能够再走了
vis[i]=1;
dfs(node[i].to);
}
cout<<u<<endl;
}
int main()
{
int u,v;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
{
scanf("%d%d",&u,&v);
addEdg(u,v);
}
dfs(1);
return 0;
}
POJ 2230 Watchcow && USACO Watchcow 2005 January Silver (欧拉回路)的更多相关文章
- usaco 月赛 2005 january watchcow
2013-09-18 08:13 //By BLADEVIL var n, m :longint; pre, other :..] of longint; last :..] of longint; ...
- usaco 月赛 2005 january sumset
2013-09-18 08:23 打表找规律 w[i]:=w[i-1]; 奇 w[i]:=w[i-1]+w[i div 2]; 偶 //By BLADEVIL var w :..] of l ...
- USACO月赛2005 january volume
2013-09-18 08:12 由题可知,ans=∑i ∑j(x[i]-x[j]) 最后整理完之后应该是不同系数的X[i]相加,所以这道题就成了求不同x[i]的系数 对于X[i],它需要减前面(i ...
- [欧拉] poj 2230 Watchcow
主题链接: http://poj.org/problem? id=2230 Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
- POJ 2230 Watchcow
Watchcow Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2 ...
- POJ 2230 Watchcow (欧拉回路)
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5258 Accepted: 2206 Specia ...
- POJ 2230 Watchcow 【欧拉路】
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6336 Accepted: 2743 Specia ...
- POJ 2230 Watchcow 欧拉图
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8800 Accepted: 3832 Specia ...
- POJ 2230 Watchcow 欧拉回路的DFS解法(模板题)
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9974 Accepted: 4307 Special Judg ...
随机推荐
- Excelutil 工具类
1.说明:ExcelUtil主要用于获得单元格的数据和对对指定单元格中写入数据用! 相关代码如下: package main.java; import java.io.File; import jav ...
- [转帖]IPV6取代IPV4之路 为何道阻且长?
IPV6取代IPV4之路 为何道阻且长? 经济学人公众号 IPV6作为IPV4的续命神术,从被提出到现今,逾26年之久.而IPV6在中国更是犹抱琵琶半遮面,千呼万唤难出来,IPV6取代IPV4之路,为 ...
- php反射方法信息
<?phpclass ReflectionFunction implements Reflector{ final private __clone() public object _ ...
- BAT等公司必问的8道Java经典面试题,你都会了吗?
工作多年以及在面试中,我经常能体会到,有些面试者确实是认真努力工作,但坦白说表现出的能力水平却不足以通过面试,通常是两方面原因: 1.“知其然不知其所以然”.做了多年技术,开发了很多业务应用,但似乎并 ...
- python的==和is区别
Python中: is判断两个标识符是否引自同一个对象 ==判断两个标识符的值是否相等 区别于java: ==判断两个标识符是否引自同一个对象 .equals()判断是否相等 #如果是String ...
- Life Forms POJ - 3294(不小于k个字符串中的最长子串)
题意: 求不小于字符串一半长度个字符串中的最长字串 解析: 论文题例11 将n个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开, 求后缀数组, 然后二分答案变为判定性问题, 然后判断每组的 ...
- 【转载】dfs序七个经典问题
作者:weeping 出处:www.cnblogs.com/weeping/ 原文链接 https://www.cnblogs.com/weeping/p/6847112.html 参考自:<数 ...
- docker attach 和 docker exec
docker attach docker attach -- Attach to a running container. 常用选项: --sig-proxy=true:Proxy all recei ...
- BZOJ 3438 小M的作物 & BZOJ 1877 [SDOI2009]晨跑
我由衷地为我的朋友高兴.哈哈,yian,当你nick name破百上千时,再打“蒟蒻”就会被打的. 好的,说正事吧.请注意,这还是题解.但我发现,网络流实在是太套路了(怪不得这两年几乎销声匿迹).我们 ...
- [JSOI2009] 球队收益 (费用流)
终于来发题解啦! pdf版题解 #include<iostream> #include<cstring> #include<cstdio> #include< ...