题目链接:http://poj.org/problem?id=1041

题目:

题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发又回到起点的字典序最小的路径,如果达不到输出Round trip does not exist.

思路:首先得判断是否存在欧拉回路,如果不存在则输出“Round trip does not exist.”。记录每个路口的度,如果存在度为奇数得路口则是不存在欧拉回路得图,否则用mp[u][t]=v来表示u可以通过t这条街道到达v,跑一边欧拉回路并记录路径即可。

代码实现如下:

 #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 = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int s, u, v, t, mx, p;
int mp[][], in[], vis[], ans[]; void eulergraph(int s) {
for(int i = ; i <= mx; i++) {
if(mp[s][i] && !vis[i]) {
vis[i] = ;
eulergraph(mp[s][i]);
ans[++p] = i;
}
}
} int main() {
//FIN;
while(~scanf("%d%d", &u, &v)) {
if(u == && v == ) break;
s = min(u, v);
p = ;
memset(in, , sizeof(vis));
memset(mp, , sizeof(mp));
memset(vis, , sizeof(vis));
scanf("%d", &t);
in[u]++, in[v]++;
mx = t;
mp[u][t] = v, mp[v][t] = u;
while(~scanf("%d%d", &u, &v)) {
if(u == && v == ) break;
scanf("%d", &t);
mx = max(mx, t);
in[u]++, in[v]++;
mp[u][t] = v, mp[v][t] = u;
}
int flag = ;
for(int i = ; i <= ; i++) {
if(in[i] & ) {
printf("Round trip does not exist.\n");
flag = ;
break;
}
}
if(flag) continue;
eulergraph(s);
for(int i = p; i >= ; i--) {
printf("%d%c", ans[i], i == ? '\n' : ' ');
}
}
return ;
}

John's trip(POJ1041+欧拉回路+打印路径)的更多相关文章

  1. UVA302 John's trip(欧拉回路)

    UVA302 John's trip 欧拉回路 attention: 如果有多组解,按字典序输出. 起点为每组数据所给的第一条边的编号较小的路口 每次输出完额外换一行 保证连通性 每次输入数据结束后, ...

  2. Uva 10054 欧拉回路 打印路径

    看是否有欧拉回路 有的话打印路径 欧拉回路存在的条件: 如果是有向图的话 1.底图必须是连通图 2.最多有两个点的入度不等于出度 且一个点的入度=出度+1 一个点的入度=出度-1 如果是无向图的话 1 ...

  3. poj1041 John's trip——字典序欧拉回路

    题目:http://poj.org/problem?id=1041 求字典序欧拉回路: 首先,如果图是欧拉图,就一定存在欧拉回路,直接 dfs 即可,不用 return 判断什么的,否则TLE... ...

  4. Watchcow(POJ2230+双向欧拉回路+打印路径)

    题目链接:http://poj.org/problem?id=2230 题目: 题意:给你m条路径,求一条路径使得从1出发最后回到1,并满足每条路径都恰好被沿着正反两个方向经过一次. 思路:由于可以回 ...

  5. POJ1041 John's trip

    John's trip Language:Default John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  6. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. BFS+打印路径

    题目是给你起点sx,和终点gx:牛在起点可以进行下面两个操作: 步行:John花一分钟由任意点X移动到点X-1或点X+1. 瞬移:John花一分钟由任意点X移动到点2*X. 你要输出最短步数及打印路径 ...

  8. Java实现John's trip(约翰的小汽车)

    1 问题描述 John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8998 Accepted: 3018 Sp ...

  9. LCS(打印路径) POJ 2250 Compromise

    题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...

随机推荐

  1. Spring Boot(二)配置分析

    回顾一下采用SSM开发项目时,项目中会存在多个配置文件,比如web.xml,配置Spring相关的applicationContext-springmvc.xml, applicationContex ...

  2. exception = {"元数据集合中已存在具有标识“xxx”的项。\r\n参数名: item"}

    vs提示:exception = {"元数据集合中已存在具有标识"xxx"的项.\r\n参数名: item"} 出现这个错误说明有重复的字段,有可能是继承的类里 ...

  3. 【linux】- nohup 和 &

    &的意思是在后台运行, 什么意思呢? 意思是说,当你在执行 ./a.out & 的时候,即使你用ctrl C,那么a.out照样运行(因为对SIGINT信号免疫).但是要注意,如果你直 ...

  4. dedecms给原模型添加新字段

    1.进入dedecms后台 2.点击核心=>频道模型=>内容模型管理(在这里可以看到dedecms预设的模型设置) 3.选中我们需要的模型,点击更改,跳入以下页面 4.点击字段管理(可以看 ...

  5. ICE checkbox 用法

    Hello everybody, I have a datable which contain multiple lines gotten from database, in the header o ...

  6. 学习 SQL 语句 - Select(9): 其他

    //只要前五条记录 procedure TForm1.Button1Click(Sender: TObject); begin   with ADODataSet1 do begin     Clos ...

  7. jsp文件过大,is exceeding 65535 bytes limit

    今天修改配置项的时候,遇到了一个异常,Generated servlet error:The code of method _jspService(HttpServletRequest, HttpSe ...

  8. 基于jquery的移动端JS无缝切换

    Html: <div id="slide-box-1"> <ul> <li> <a href="javascript:void( ...

  9. poj2018——Best Cow Fences

    Description Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each fie ...

  10. BZOJ3172:[TJOI2013]单词——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3172 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单 ...