题目链接

求给出的图是否存在欧拉回路并输出路径, 从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. Data visualization 课程 笔记3

    Learn how humans work to create a more effective computer interface 三种reasoning的方式  Deductive Reason ...

  2. Sequence operation(线段树区间多种操作)

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. C#经典之Application.DoEvents()的使用

    最近做了一个文件上传的模块,因为牵扯到电脑文件的扫描,想做一个实时显示当前扫面文件的功能,就类似于360文件扫描时的效果,本来打算用多线程来实现,但是方法太多没有实现,后来在程序中进行控制,由于文件太 ...

  4. ul li 好友列表

    <div class="col-md-4 mt"> <div class="friend-list"> <h4 class=&qu ...

  5. DataGridView插入一行数据和用DataTable绑定数据2种方式

    以前不会用DataGridView的时候一直使用DataTable绑定的方式,如下: DataTable table = new DataTable(); //给表添加一列Name,此名字和 tabl ...

  6. MVC打开电脑对话框

    //下载文件 public ActionResult Download(int id) { //依靠模板生成文档 var path =要下载的文件的路径 var name = Path.GetFile ...

  7. hadoop搭建杂记:Linux下ssh免密码登陆

    关于ssh免密码登陆的问题 关于ssh免密码登陆的问题 linux下可以用ssh-keygen来生成公钥/私钥对 ①生成id_rsa和id_rsa.pub公钥/私钥对,自动在~/.ssh下生成文件(亦 ...

  8. 1)③爬取网易It方面部分新闻

    __author__ = 'minmin' #coding:utf-8 import re,urllib,sgmllib,os #根据当前的url获取html def getHtml(url): pa ...

  9. vcredist作用

    一.vcredist作用: vcredist_x86.exe是微软公司Visual C++的32位运行时库,包含了一些Visual C++的库函数. vcredist_x64.exe是微软公司Visu ...

  10. windows server system32下常见快捷指令

    win+R       命令行窗口 cmd        dos命令窗口 mstsc      远程登录输入窗口 calc         快速打卡计算器 control     打开控制面板 eve ...