欧拉回路输出(DFS,不用回溯!)Watchcow POJ 2230
| Time Limit: 3000MS | Memory Limit: 65536K | |||
| Total Submissions: 8109 | Accepted: 3551 | Special Judge | ||
Description
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
* Lines 2..M+1: Two integers denoting a pair of fields connected by a path.
Output
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
Bessie starts at 1 (barn), goes to 2, then 3, etc...
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 10009
#define N 50009
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-;
const double PI = acos(-1.0); struct edge
{
edge(int _v, bool _vis) :v(_v), vis(_vis){}
int v;
bool vis;
};
vector<edge> E[MAXN];
int n, m;
void DFS(int cur)
{
for (int i = ; i < E[cur].size(); i++)
{
if (!E[cur][i].vis)
{
E[cur][i].vis = true;
DFS(E[cur][i].v);
}
}
printf("%d\n", cur);
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = ; i <= n; i++)
E[i].clear();
int f, t;
for (int i = ; i < m; i++)
scanf("%d%d", &f, &t), E[f].push_back(edge(t,false)), E[t].push_back(edge(f,false));
DFS();
}
}
上面是有向图的回溯
下面是无向图。从度最大的点往前回溯
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<map>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 55
#define N 2000
typedef long long LL; /*
无向图的欧拉回路
从度最大的点开始回溯
*/
int T, n;
int g[MAXN][MAXN];
int degree[MAXN];
void dfs(int k)
{
for (int i = ; i <= ; i++)
{
if (g[k][i])
{
g[k][i]--, g[i][k]--;
dfs(i);
printf("%d %d\n", i, k);
}
}
}
int main()
{
scanf("%d", &T);
for (int cas = ; cas <= T; cas++)
{
memset(degree, , sizeof(degree));
memset(g, , sizeof(g));
scanf("%d", &n);
for (int i = ; i < n; i++)
{
int a, b;
scanf("%d%d", &a, &b);
g[a][b]++, g[b][a]++;
degree[a]++, degree[b]++;
}
int Max = -, k = -;
bool f = true;
for (int i = ; i < MAXN; i++)
{
if (degree[i] > Max)
{
Max = degree[i], k = i;
}
if (degree[i] % == )
{
f = false;
break;
}
}
printf("Case #%d\n", cas);
if (f)
dfs(k);
else
printf("some beads may be lost\n");
if (cas <= T)
printf("\n"); }
}
欧拉回路输出(DFS,不用回溯!)Watchcow POJ 2230的更多相关文章
- Day4 - D - Watchcow POJ - 2230
Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the ...
- poj 1041(欧拉回路+输出字典序最小路径)
题目链接:http://poj.org/problem?id=1041 思路:懒得写了,直接copy吧:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路.一个无向图存 ...
- POJ 2230 DFS
题意: Bessie 最近做了农场看守,他每天晚上的工作就是巡视农场并且保证没有坏人破坏农场.从谷仓出发去巡视,并且最终回到谷仓. Bessie 视力不是很好,不能像其他农场的看守一样,对农场的每一条 ...
- 洛谷1378 油滴扩展 dfs进行回溯搜索
题目链接:https://www.luogu.com.cn/problem/P1378 题目中给出矩形的长宽和一些点,可以在每个点放油滴,油滴会扩展,直到触碰到矩形的周边或者其他油滴的边缘,求出剩余面 ...
- POJ 2230 Watchcow(欧拉回路:输出点路径)
题目链接:http://poj.org/problem?id=2230 题目大意:给你n个点m条边,Bessie希望能走过每条边两次,且两次的方向相反,让你输出以点的形式输出路径. 解题思路:其实就是 ...
- POJ 2230 Watchcow 欧拉回路的DFS解法(模板题)
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9974 Accepted: 4307 Special Judg ...
- POJ 2230 Watchcow(有向图欧拉回路)
Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the ...
- 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 wal ...
- POJ 2230 Watchcow (欧拉回路)
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5258 Accepted: 2206 Specia ...
随机推荐
- 怎样去思考问题 解决问题 zkc学长的福利
题目描述 又是一年ACM集训的时刻,zkc学长邀请n位同学来玩一个有奖游戏.首先,他让每个同学在左.右手上面分别写下一个整数,zkc学长自己也在左.右手上各写一个整数.然后,让这n位同学排成一排,zk ...
- 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game
题目传送门 /* 贪心:暴力贪心水水 */ #include <cstdio> #include <algorithm> #include <cstring> us ...
- STL内存分配方式
关于STL用的很多比如map, vector, set, queue, stack等等.很少关注它的内存分配情况,但是经常遇到比如使用map,不停的在map中插入了一些很小的对象,然后释放了一些,然后 ...
- [转]ASP.NET MVC HtmlHelper扩展之Calendar日期时间选择
本文转自:http://blog.bossma.cn/asp_net_mvc/asp-net-mvc-htmlhelper-calendar-datetime-select/ 这里我们扩展HtmlHe ...
- C#模拟百度登录并到指定网站评论回帖(四)
基本的实现功能前面已经全部讲完,基本上可以复制黏贴完成登录百度的过程了 今天的这一贴就说说怎么获取百度的验证码 内容回顾:还记得前面第一贴说的如果登录发生异常,百度会发回2个值吗?是的,就是codeT ...
- css的新特性 calc () 使用
calc()对大家来说,或许很陌生,不太会相信calc()是css中的部分.因为看其外表像个函数,既然是函数为何又出现在CSS中呢?这一点也让我百思不得其解,今天有一同事告诉我,说CSS3中有一个属性 ...
- 对SNL语言的解释器实现尾递归优化
对于SNL语言解释器的内容可以参考我的前一篇文章<使用antlr4及java实现snl语言的解释器>.此文只讲一下"尾递归优化"是如何实现的--"尾递归优化& ...
- Chromium浏览器编译成功庆祝
1.什么是Chromium Chromium 是Google公司的开源项目 Google浏览器 最新版360浏览器 都是在Chromium的基础上重新编译的. 2.什么是双核浏览器 ...
- Django框架 之基础入门
django是一款MVT的框架 一.基本过程 1.创建项目:django-admin startproject 项目名称 2.编写配置文件settings.py(数据库配置.时区.后台管理中英文等) ...
- 牛客多校Round 9
Solved:1 rank:112 E. Music Game 题解说有个非简化的原题 bzoj4318 #include <bits/stdc++.h> using namespace ...