POJ2230 Watchcow 【欧拉回路】+【DFS】

Watchcow

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

题意:给定n个点和m条路,求从1点出发每条路的两个方向都走一遍再回到原点的路径。

题解:将无向图转换成有向图,DFS遍历整张图,每条边只能走一次。

#include <stdio.h>
#include <string.h>
#define maxn 10002
#define maxm 50002

int n, m;
int head[maxn];
bool vis[maxm << 1];
struct Node{
	int to, next;
} edge[maxm << 1];

void Init()
{
	memset(head, -1, sizeof(head));
	memset(vis, 0, sizeof(vis));
}

void GetEdge()
{
	int i, a, b;
	m <<= 1;
	for(i = 0; i < m; ++i){
		scanf("%d%d", &a, &b);
		edge[i].to = b;
		edge[i].next = head[a];
		head[a] = i++;
		edge[i].to = a;
		edge[i].next = head[b];
		head[b] = i;
	}
}

void DFS(int k)
{
	for(int i = head[k]; i != -1; i = edge[i].next){
		if(!vis[i]){
			vis[i] = 1;
			DFS(edge[i].to);
		}
	}
	printf("%d\n", k);
}
int main()
{
	int i;
	while(scanf("%d%d", &n, &m) == 2){
		Init();
		GetEdge();
		DFS(1);
	}
	return 0;
}

POJ2230 Watchcow 【欧拉回路】+【DFS】

时间: 2024-08-28 05:52:33

POJ2230 Watchcow 【欧拉回路】+【DFS】的相关文章

POJ 2230 Watchcow 欧拉回路题解

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

POJ2230 Watchcow【欧拉回路】

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

poj2230 Watchcow(欧拉回路)

题    目    传    送    门    在    这 题目大意 有一个由n个点和m条边组成的无向图. 让你从节点1出发,每条边恰好从正反两个方向走一遍. 输出任意一种方案. 解题思路 那这题和欧拉回路对比一下,发现欧拉回路是每个边直走一次,所以有以下代码. vis[e] = vis[e ^ 1] = 1; 其中标记了当的方向和反边. 而这题是从两个方向走,所以对每个边只要标记以下当前的方向,每个边就会遍历两遍. 代码如下: #include <iostream> #include &

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.

POJ2230(打印欧拉回路)

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

hdu1878欧拉回路(DFS+欧拉回路)

欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10633    Accepted Submission(s): 3875 Problem Description 欧拉回路是指不令笔离开纸面.可画过图中每条边仅一次,且能够回到起点的一条回路.现给定一个图.问是否存在欧拉回路? Input 測试输入包括若干測试用例.每一个測试用

欧拉路,欧拉回路小结(转)

欧拉路,欧拉回路小结 把欧拉路和欧拉回路做一个小总结,包含了一些题目,以后遇到新的我还会陆续加上. 定义: 给定无孤立结点图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在 一条回路经过G每条边有且仅有一次,称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 关于欧拉路和欧拉回路定义及存在证明请看这里. 这里给出欧拉路和欧拉回路存在条件的结论: 存在欧拉路的条件: 无向图:  图连通,所有点都是偶数度,或者只有两个点是奇数度.当所有点是偶数度时欧拉路起点可以是任意 点:当

Little Sub and Traveling(杭师大第十二届校赛E题) 欧拉回路

题目传送门 题目大意: 从0出发,每次只能跳到(i*2)%n或者(i*2+1)%n,求字典序最大的哈密顿回路. 思路: 首先n为奇数时无解,先来证明这一点. 先假设n为奇数,若要回到原点,则必定有一步是$a%n=0$,则$a=kn(k为整数)$. 我们先假设a是通过$2x$的方式得到的,即$kn=2*x$,由于此时$n$为奇数,则k必定为偶数,由于$n>x$,所以$k<2$,k又不能等于0,所以此时k无解,a不是通过$s*x$的方法得到的 那么$kn=2*x+1$,k为奇数,则$k=1  ,a

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小