Watchcow(POJ2230+双向欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=2230
题目:


题意:给你m条路径,求一条路径使得从1出发最后回到1,并满足每条路径都恰好被沿着正反两个方向经过一次。
思路:由于可以回到起点,并且题目保证有解,所以本题是欧拉回路。输出路径有两种方法,一种是递归实现,一种是用栈处理,不过两种速度差了1s,不知道是不是我递归没处理好~

代码实现如下(第一种为栈输出路径,对应797ms,第二种为递归,对应1782ms):
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 5e4 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, m, u, v, tot, top, t;
int head[maxn], stc[maxn*], ans[maxn*], vis[maxn*]; struct edge {
int v, next;
}ed[maxn*]; void addedge(int u, int v) {
ed[tot].v = v;
ed[tot].next = head[u];
head[u] = tot++;
ed[tot].v = u;
ed[tot].next = head[v];
head[v] = tot++;
} void eulergraph() {
stc[++top] = ;
while(top > ) {
int x = stc[top], i = head[x];
while(i != - && vis[i]) i = ed[i].next;
if(i != -) {
stc[++top] = ed[i].v;
head[x] = ed[i].next;
vis[i] = ;
} else {
top--;
ans[++t] = x;
}
}
} int main() {
//FIN;
scanf("%d%d", &n, &m);
tot = top = t = ;
memset(stc, , sizeof(stc));
memset(ans, , sizeof(ans));
memset(vis, , sizeof(vis));
memset(head, -, sizeof(head));
for(int i = ; i <= m; i++) {
scanf("%d%d", &u, &v);
addedge(u, v);
}
eulergraph();
for(int i = t; i; i--) printf("%d\n", ans[i]);
}
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 1e4 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, m, u, v, tot, top, t;
int head[maxn], vis[maxn*]; struct edge {
int v, next;
}ed[maxn*]; void addedge(int u, int v) {
ed[tot].v = v;
ed[tot].next = head[u];
head[u] = tot++;
ed[tot].v = u;
ed[tot].next = head[v];
head[v] = tot++;
} void eulergraph(int x) {
for(int i = head[x]; ~i; i = ed[i].next) {
if(!vis[i]) {
vis[i] = ;
eulergraph(ed[i].v);
}
}
printf("%d\n", x);
} int main() {
//FIN;
scanf("%d%d", &n, &m);
tot = top = t = ;
memset(vis, , sizeof(vis));
memset(head, -, sizeof(head));
for(int i = ; i <= m; i++) {
scanf("%d%d", &u, &v);
addedge(u, v);
}
eulergraph();
return ;
}
Watchcow(POJ2230+双向欧拉回路+打印路径)的更多相关文章
- Uva 10054 欧拉回路 打印路径
看是否有欧拉回路 有的话打印路径 欧拉回路存在的条件: 如果是有向图的话 1.底图必须是连通图 2.最多有两个点的入度不等于出度 且一个点的入度=出度+1 一个点的入度=出度-1 如果是无向图的话 1 ...
- John's trip(POJ1041+欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...
- UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- LCS(打印路径) POJ 2250 Compromise
题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...
- UVA 624 (0 1背包 + 打印路径)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...
- zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...
- AOE网上的关键路径(最长路径 + 打印路径)
题目描述 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图. AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG ...
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径
推荐一篇炒鸡赞的blog. 以下代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring& ...
随机推荐
- PART1 一些想法
其实我一直是一个后知后觉的人,这点也是我过了好久才发现的问题,之所以晚发现自己这个毛病,是因为后知后觉==,这有点像是个悖论或者是笑话,但的确是真实存在于我的身上.其实当初为啥来这个学校选计算机的专业 ...
- 转Web开发的发展史---Web开发技术的演变
转自:http://blog.csdn.net/zzzkk2009/article/details/9849431 在接下来的几个月时间里,我打算写一系列关于完整web开发的文章.这第一篇文章虽然有所 ...
- 如何彻底解决adb 5037端口被占用
在进行安卓开发的时候是不是经常碰到adb端口被占用的情况? 解决这个问题的方法很简单,只需要设置一个系统环境变量就可以搞定. 设置方法: 增加系统环境变量变量名称:ADNROID_ADB_SERVER ...
- c++:error2019,无法解析的外部命令blabla~
出现这个原因的问题汇总: 1,相应的附加库没有包含进去,注意附加库的目录是 / 2,函数没有与之对应的类,却在main中以某一类的对象调用了该方法. 其实,当错误中显示fun()成为无法解析的外部命令 ...
- [Leetcode] 3.Longest Substring Without Repeating Characters(unordered_map)
通过把未访问的结点放到unordered_map中来判断是否重复,代码如下: class Solution { public: int lengthOfLongestSubstring(string ...
- CCS3 动画-鼠标放上去放大背景图片
---〉 效果如上,一个简单的过渡放大效果, <!DOCTYPE HTML> <html> <body> <style> #test{ width:30 ...
- nopi导出
1.NPOI官方网站:http://npoi.codeplex.com/ 可以到此网站上去下载最新的NPOI组件版本 2.NPOI在线学习教程(中文版): http://www.cnblogs.com ...
- asp.net AES加密跟PHP的一致,将加密的2进制byte[]转换为16进制byte[] 的字符串获得
<?php class AESUtil { public static function encrypt($input, $key) { $size = mcrypt_get_block_siz ...
- JS作用域-面向对象
1. 其它语言是以代码块作为作用域的.下面程序会报错(如C,C++中),因为局部变量name只在{ }代码块中生效.打印console.writeline(name)中的name时就会报错. pu ...
- 前端开发学习之——dom ready和window onload的区别
1.ready事件是在页面中所有DOM结构已完全加载时执行,监听的是 DomContentload 事件,初始化并解析完成时触发,不需要等待样式表.图片和 iframes 加载完,也就是说当这个事件触 ...