题目链接

求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出。

将每个点的边排序一下就可以。

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
vector <pll> v[];
int de[], cnt, ans[], vis[];
void add(int a, int b, int c) {
v[a].pb(mk(c, b));
de[a]++;
}
void dfs(int u) {
int len = v[u].size();
for(int i = ; i<len; i++) {
int to = v[u][i].second;
int e = v[u][i].fi;
if(vis[e])
continue;
vis[e] = ;
dfs(to);
ans[cnt++] = e;
}
}
int main()
{
int a, b, c;
while() {
scanf("%d%d", &a, &b);
if(a+b==)
break;
scanf("%d", &c);
for(int i = ; i<; i++)
v[i].clear();
mem(de);
mem(vis);
cnt = ;
add(a, b, c);
add(b, a, c);
while() {
scanf("%d%d", &a, &b);
if(a+b==)
break;
scanf("%d", &c);
add(a, b, c);
add(b, a, c);
}
int flag = ;
for(int i = ; i<=; i++) {
if(de[i]%==) {
flag = ;
break;
}
sort(v[i].begin(), v[i].end());
}
if(flag) {
puts("Round trip does not exist.");
continue;
}
dfs();
for(int i = cnt-; i>; i--)
cout<<ans[i]<<" ";
cout<<ans[]<<endl;
}
return ;
}

poj 1041 John's trip 欧拉回路的更多相关文章

  1. poj 1041 John's trip——欧拉回路字典序输出

    题目:http://poj.org/problem?id=1041 明明是欧拉回路字典序输出的模板. 优先队列存边有毒.写跪.学习学习TJ发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...

  2. POJ 1041 John's trip 无向图的【欧拉回路】路径输出

    欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为 ...

  3. [POJ 1041] John's Trip

    [题目链接] http://poj.org/problem?id=1041 [算法] 欧拉回路[代码] #include <algorithm> #include <bitset&g ...

  4. POJ 1041 John&#39;s trip Euler欧拉回路判定和求回路

    就是欧拉判定,判定之后就能够使用DFS求欧拉回路了.图论内容. 这里使用邻接矩阵会快非常多速度. 这类题目都是十分困难的.光是定义的记录的数组变量就会是一大堆. #include <cstdio ...

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

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

  6. poj 1041(字典序输出欧拉回路)

    John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8641   Accepted: 2893   Spe ...

  7. POJ1041 John's trip

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

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

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

  9. John's trip POJ - 1041(这题数据有点水)

    题意: 其实还是一个欧拉回路,但要按字典序走路: 解析: 我真是蠢啊emm... map[i][j]表示由顶点i经街道j会到达的顶点编号 然后枚举j就好了 用栈储存.. 虽然我不是这样写的 #incl ...

随机推荐

  1. [基础]RHEL6下iSCSI客户端挂载配置

    本文根据鸟哥私房菜进行操作:http://linux.vbird.org/linux_enterprise/xen.php?thisscreen=800x600 一.侦测 [root@vhost3 ~ ...

  2. Java-----Excel转HTML

    尽管是转别人的(忘了哪转过来的了),但此处标为原创不是为了提高訪问量,也不是为了其它.仅仅是纯粹的认为有实际用途.希望能给很多其它有此需求的人看到并能帮到他们就足够了 所需jar包:jxl.jar p ...

  3. 【分享】事实上,你VS界面也可以如此,VS界面美化

    阿土.它直接在地图上. 第一节目:Transformers(变形金刚) 第二方案:Assassin's Creed (刺客信条) watermark/2/text/aHR0cDovL2Jsb2cuY3 ...

  4. Android getReadableDatabase() 和 getWritableDatabase()

    Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例.(getReadableDa ...

  5. 如何查看npm配置?

    npm config list //查看基本配置 npm config list -l //查看所有配置 npm本地安装与全局安装有什么区别? npm install grunt // 本地安装,则是 ...

  6. web开发 - 从零开始 - 02 - 常用标签

    1.a  : href 属性为id,即为锚点:文件路径,则为下载链接: 新窗口打开文件,target = "_blank";当前窗口打开,target="_self&qu ...

  7. Unity5UGUI 官方教程学习笔记(二)Rect Transform

    Rect Transform Posx    Posy   Posz  :  ui相对于父级的位置 Anchors :锚点  定义了与父体之间的位置关系    一个锚点由四个锚组成  四个锚分别代表了 ...

  8. String字符串需要掌握的几个问题

    一.两种定义方式的区别: String str = "hello";      先在堆内存中查找是否已经有"hello",若有,将str指向已存在的它即可:若堆 ...

  9. 浅谈 non-blocking I/O Multiplexing + poll/epoll 的正确使用

    在前面的文章中曾经粗略讲过poll,那时是用阻塞IO实现,在发送和接收数据量都较小情况下和网络状况良好的情况下是基本没有问题的,read 不会只接收部分数据,write 也不会一直阻塞.但实际上pol ...

  10. R包——ggplot2(一)

    关于ggplot2包(一) 关于ggplot2包(一) ggplot2基本要素 数据(Data)和映射(Mapping) 几何对象(Geometric) 标尺(Scale) 统计变换(Statisti ...