(有向图欧拉回路求解) poj 2230

Watchcow

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 6255   Accepted: 2708   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.

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,m;
vector<int> e[10010];
bool vis[10010];
void dfs(int u)
{
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(v!=-1)
        {
            e[u][i]=-1;
            dfs(v);
        }
    }
    printf("%d\n",u);
}
int main()
{
    int x,y;
    memset(vis,0,sizeof(vis));
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        e[x].push_back(y);
        e[y].push_back(x);
    }
    dfs(1);
    return 0;
}

  

时间: 2024-08-02 07:02:12

(有向图欧拉回路求解) poj 2230的相关文章

算法笔记_148:有向图欧拉回路求解(Java)

目录 1 问题描述 2 解决方案   1 问题描述 Description A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For example, the following are catenyms: dog.gopher gopher.rat rat.tige

POJ 2230 Watchcow 欧拉回路题解

本题就是以每个节点和节点之间建路,而且说明是无向图,不过这里有个技巧,就是根据题意把它当成有向图来做,就成了直接查找有向图的欧拉回路就可以了.因为题意是需要每条边都走两遍的,而且每次走的方向相反. 观察出这点,那么这道题就好做啦,直接建图,Feury递归求解就可以了. 建图注意需要建邻接表,不要建矩阵,因为建成矩阵,那么会很大很大,而根据题意,建成邻接表最多只需要5倍的顶点数. 打印的顺序是逆过来打和顺着打都可以的,因为先走那边都可以. #include <stdio.h> #include

POJ 2230 Watchcow &amp;&amp; USACO Watchcow 2005 January Silver (欧拉回路)

题意: Bessie 最近做了农场看守,他每天晚上的工作就是巡视农场并且保证没有坏人破坏农场.从谷仓出发去巡视,并且最终回到谷仓. Bessie 视力不是很好,不能像其他农场的看守一样,对农场的每一条连接不同场地的路走一遍就可以发现是不是有异常情况,他需要每条路都走两遍,并且这两边必须是不同的方向,因为他觉得自己应该不会两次都忽略农场中的异常情况. 每块地之间一定会由至少一条路相连.现在的任务就是帮他制定巡视路线.前提假设一定存在满足题意的路径. 输入: 第一行输入两个数N(2 <= N <=

POJ 2230 Watchcow (欧拉回路)

题目地址:POJ 2230 最普通的欧拉回路.边不重复记录点.不多说. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.

poj 1386 Play on Words(有向图欧拉回路)

1 /* 2 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 3 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 4 5 再次强调有向图欧拉路或欧拉回路的判定方法: 6 (1)有向图G为欧拉图(存在欧拉回路),当且仅当G的基图连通,且所有顶点的入度等于出度. 7 (2)有向图G为半欧拉图(存在欧拉道路),当且仅当G的基图连通,且存在顶点u的入度比出度大1.v的入度比出度小1, 8 其它所有顶点的入度等于出度(顶点u,v的个数必须都是

[2016-01-27][POJ][2230][Watchcow]

H - 欧拉图 中级者向 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2230 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 s

POJ 2230 解题报告

分析: 基础的欧拉路算法,变化在于要求每条边正向和反向各走一遍. 链式前向星构图,只要标记走过的单向边,边找边输出即可. code #include <iostream> #include <cstdio> using namespace std; struct node { int v, ne; } edge[100009]; int head[10009], vis[100009], cnt = 1; int n, m, x, y; void addedge (int u, i

[欧拉回路] poj 2230 Watchcow

题目链接: http://poj.org/problem?id=2230 Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6055   Accepted: 2610   Special Judge Description Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk acr

欧拉回路求路径POJ 2230

Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8841   Accepted: 3854   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 ev