题目链接

求给出的图是否存在欧拉回路并输出路径, 从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. ASPxGridView-为每行添加序号

    添加一个新的非绑定列,使用CustomColumnDisplayText事件来分配序号给该列 <dx:GridViewDataTextColumn Caption="序号" ...

  2. C++学习之运算符重载的总结

    C++学习之运算符重载的总结              运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用域不同类型的数据导致不同行为的发生,C++为运算符重载提供了一种方法,即运算符重载函数 ...

  3. Hadoop学习之自定义二次排序

    一.概述    MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往 往有要对reduce输出结果进行二次排 ...

  4. 多个tab切换传参

    var _ =peopleslive.h5; _.main=function(){ //集赞排名1 $('#tab1 .tab_nav').find('li').click (function() { ...

  5. codeforces 631C. Report

    题目链接 按题目给出的r, 维护一个递减的数列,然后在末尾补一个0. 比如样例给出的 4 21 2 4 32 31 2 递减的数列就是3 2 0, 操作的时候, 先变[3, 2), 然后变[2, 0) ...

  6. bzoj 1085: [SCOI2005]骑士精神 IDA*

    题目链接 给一个图, 目标位置是确定的, 问你能否在15步之内达到目标位置. 因为只有15步, 所以直接ida* #include<bits/stdc++.h> using namespa ...

  7. underscore api 概览

    underscore 集合函数(数组或对象) _.each(list, iteratee, [context]); _.map(list, iteratee, [context]); _.reduce ...

  8. IC封装

    1.QFN •QFN—Quad Flat No-lead Package 四方无引脚扁平封装 2.SOIC •SOIC—Small Outline IC 小外形IC封装 3.TSSOP •TSSOP— ...

  9. Codeforces 700B Connecting Universities(树形DP)

    [题目链接] http://codeforces.com/problemset/problem/700/B [题目大意] 给出 一棵n个节点的树, 现在在这棵树上选取2*k个点,两两配对,使得其配对的 ...

  10. BZOJ 1096 [ZJOI2007]仓库建设(斜率优化DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1096 [题目大意] 有个斜坡,有n个仓库,每个仓库里面都有一些物品,物品数目为p,仓库 ...