欧拉回路输出(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 ...
随机推荐
- 【BZOJ4059】Non-boring sequences(分析时间复杂度)
题目: BZOJ4059 分析: 想了半天没什么想法,百度到一个神仙做法-- 设原数列为 \(a\),对于每一个 \(i\) 求出前一个和后一个和 \(a_i\) 相等的位置 \(pre[i]\) 和 ...
- 最大化最小值poj2456Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15528 Accepted: 7440 ...
- DHTML_____window对象属性
<html> <head> <meta charset="utf-8"> <title>window对象属性</title&g ...
- 403 Frog Jump 青蛙过河
一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定 ...
- python获取主机名和用户名
import socketimport getpassuser_name = getpass.getuser() # 获取当前用户名hostname = socket.gethostname() # ...
- layer父页获取弹出层输入框里面的值
主要是因为修改功能,原来页面填写数据如图 改为 其中点击填写明细弹出框 填写完毕后点击确认返回,同事这里因为她是存的多表,所以点击确认就直接保存数据了,改的这个功能原本保存是整体保存,我就不想改原来的 ...
- html与html5 总结
时间:于2017年12月3日 19:35:18开始 用于: 这文章用于个人技术总结(全文转发请标明出处,小段摘抄随意) HTML笔记1.html标签不区分大小写,但是建议小写2.常用标签: 可用在he ...
- phpstorm设置代码片段
tab 向后推进 shift+tab 向前推进 ctrl+d 复制整行 ctrl+Y删除整行 代码片段就是代码快捷键,如果你设置了www.baidu.com这些内容,但是不想一直重复的打,可以设置个代 ...
- axios 正确打开方式
一.安装1. 利用npm安装npm install axios --save2. 利用bower安装bower install axios --save3. 直接利用cdn引入<script s ...
- Layui数据表单的编辑
使用layui对单元格进行编辑并保存 先是要引入layui的JS和CSS 然后创建一个表格 而重要的是edit这个属性,只有使用了这个属性的一列数据表格才可以编辑,其余的都不可以进行编辑 然后使用la ...