poj 1041 John's trip 欧拉回路

时间:2022-09-20 23:39:39

题目链接

求给出的图是否存在欧拉回路并输出路径, 从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&&num;39&semi;s trip——欧拉回路字典序输出

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

  2. POJ 1041 John&&num;39&semi;s trip 无向图的【欧拉回路】路径输出

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

  3. &lbrack;POJ 1041&rsqb; John&&num;39&semi;s Trip

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

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

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

  5. UVA302 John&&num;39&semi;s trip(欧拉回路)

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

  6. poj 1041&lpar;字典序输出欧拉回路&rpar;

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

  7. POJ1041 John&&num;39&semi;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&&num;39&semi;s trip POJ - 1041(这题数据有点水)

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

随机推荐

  1. JavaScript学习笔记&lpar;一&rpar;——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

  2. HTML基础—插曲

    HTML基础学习 1:我们在网上添加图片的时候最好是缩略图,而不是直接在代码中限制图片的大小.可以为了用户减少流量.Alt=""属性是为了让图片在现实不出来时显示的文字,Title ...

  3. JS函数和变量

    JS函数和变量  函数: 函数是由事件或者当它被调用时执行的可重复使用的代码块. 是一个独立的代码块,实现特定功能模块. 函数他不进行调用触发的话,不会自己主动执行. 像ATM机一样,不去取钱的话不会 ...

  4. ASP&period;NET fails to detect Internet Explorer 10

    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9"> http://www.han ...

  5. Linux安装JBOSS

    一.简介 JBoss是一个运行EJB的J2EE应用服务器.它是开放源代码的项目,遵循最新的J2EE规范.从JBoss项目开始至今,它已经从一个EJB容器发展成为一个基于的J2EE的一个web 操作系统 ...

  6. vue中如何获取后台数据

    原文链接:http://blog.csdn.net/vergilgeekopen/article/details/68954940 需要引用vue-resource 安装请参考https://gith ...

  7. 代码图片转文本--阿里VS度娘

    最近看vue的书,居然没有提供源代码,一堆的CSS真不想手工录入,于是用手机找APP去转.发现广告普遍,于是找找网上相关的API,结果百度和阿里都有在线的API提供,于是好奇其能力如何.如于用以下两图 ...

  8. H5手机移动端调起浏览器(qq浏览器,uc浏览器)自带分享功能实例

    H5手机移动端调起浏览器(qq浏览器,uc浏览器)自带分享功能实例 (转载:https://blog.csdn.net/weixin_38787928/article/details/86741227 ...

  9. P2043 质因子分解(阶乘的质因数分解)

    P2043 质因子分解 对$n!$进行质因数分解的一种高效算法 首先,筛出$<=n$的素数 蓝后,对$n$反复除以$prime$,同时$cnt+=n/prime$ $n!$中含有该$prime$ ...

  10. tomcat server 报错之 More than the maximum allowed number of cookies

    More than the maximum allowed number of cookies EVERE: Error processing request java.lang.IllegalArg ...