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 ...
随机推荐
- pygame学习笔记(1)——安装及矩形、圆型画图
pygame是一个设计用来开发游戏的python模块,其实说白了和time.os.sys都是一样的东东.今天开始正式学习pygame,下载地址:www.pygame.org.下载后安装完成即可,在py ...
- XMind2TestCase:一个高效测试用例设计的解决方案!
一.背景 软件测试过程中,最重要.最核心就是测试用例的设计,也是测试童鞋.测试团队日常投入最多时间的工作内容之一. 然而,传统的测试用例设计过程有很多痛点: 1.使用Excel表格进行测试用例设计,虽 ...
- 【uoj#143】[UER #5]万圣节的数列 构造
题目描述 给出一个的数列,将其重新排列,使得其等差子序列的数目最小.输出一种可能的排列后的数列. 题解 构造 那天和 EdwardFrog 讨论 bzoj2124 的构造时突然有的灵感,最后发现就是这 ...
- DPM(Deformable Parts Model)
DPM(Deformable Parts Model) Reference: Object detection with discriminatively trained partbased mode ...
- BZOJ3267/3272 KC采花/Zgg吃东西(线段树)
直接维护选k个子段时的最优解似乎也可以做,然而复杂度是O(nk2logn),显然跑不过. 考虑一种费用流做法.序列里每个点拆成入点和出点,源连入汇连出,入点和出点间连流量1费用ai的边,相邻点出点向入 ...
- P2461 [SDOI2008]递归数列
题目描述 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj 和 cj ...
- NOIP赛前集训营-提高组(第一场)#A 中位数
题目描述 小N得到了一个非常神奇的序列A.这个序列长度为N,下标从1开始.A的一个子区间对应一个序列,可以由数对[l,r]表示,代表A[l], A[l + 1], ..., A[r]这段数.对于一 ...
- hdu-3308 LCIS (线段树区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 解题:NOI 1999 生日蛋糕
题面 裸的搜索题,就说剪枝(注:nw->noww->当前,res->rest->剩余): 1.想达到$Nπ$的体积,那么半径一开始最多也就$sqrt(n)$了,再大就超了... ...
- maven使用内嵌tomcat7
在web项目的pom.xml中添加如下: <build> <plugins> <plugin> <groupId>org.apache.tomcat.m ...