poj1041 John's trip——字典序欧拉回路
题目:http://poj.org/problem?id=1041
求字典序欧拉回路;
首先,如果图是欧拉图,就一定存在欧拉回路,直接 dfs 即可,不用 return 判断什么的,否则TLE...
所以还是模板比较好啊;
字典序有点麻烦,一开始我用优先队列啦各种各样的超级麻烦,但是其实先存下读进来的边,一边放进优先队列里排序,然后倒着连上,那么它就会正着遍历了;
网上的 TJ 都是存进二维数组里桶排,也还行吧,但总感觉不够优秀啊,时间和空间都;
得到了字典序加边的方法!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,st,deg[],ans[],top,tot,map[][];//ans不是20!!!
bool vis[];
void print()
{
for(int i=top;i;i--)printf("%d ",ans[i]);
printf("\n");
}
void insert(int x,int y,int z)
{
map[x][z]=y; map[y][z]=x;
tot++; deg[x]++; deg[y]++; n=max(n,(max(x,y)));
} void dfs(int x)
{
for(int i=;i<=tot;i++)
{
if(vis[i]||!map[x][i])continue;
vis[i]=; dfs(map[x][i]);
ans[++top]=i;
}
}
int main()
{
int x,y,z;
while()
{
scanf("%d%d",&x,&y);
if(!x&&!y)return ;
memset(map,,sizeof map);
memset(deg,,sizeof deg);
memset(vis,,sizeof vis);
top=; n=; tot=;
scanf("%d",&z); st=min(x,y);//
insert(x,y,z);
while()
{
scanf("%d%d",&x,&y);
if(!x&&!y)break;
scanf("%d",&z);
insert(x,y,z);
}
bool fl=;
for(int i=;i<=n;i++)
if(deg[i]%){printf("Round trip does not exist.\n"); fl=; break;}
if(fl)continue;
dfs(st); print();
}
}
模仿TJ
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,st,hd[],ct=,deg[],ans[],top,tot;
bool vis[];
struct N{
int to,nxt,bh;
N(int t=,int n=,int b=):to(t),nxt(n),bh(b) {}
}ed[];
priority_queue<pair<int,int> >q[];
void insert(int x,int y,int z)
{
q[x].push(make_pair(z,y)); q[y].push(make_pair(z,x));
tot++; deg[x]++; deg[y]++; n=max(n,(max(x,y)));
}
void add()
{
for(int i=;i<=n;i++)
while(q[i].size())
{
int bh=q[i].top().first,to=q[i].top().second; q[i].pop();
ed[++ct]=N(to,hd[i],bh); hd[i]=ct;
}
}
void print()
{
for(int i=top;i;i--)printf("%d ",ans[i]);
printf("\n");
}
//bool dfs(int x)
//{
// if(top==tot){print(); return 1;}
// int t=0;
// for(int i=hd[x];i;i=ed[i].nxt)
// {
// if(vis[ed[i].bh])continue;
// ans[++top]=ed[i].bh; vis[ed[i].bh]=1;
// if(dfs(ed[i].to))return 1;
// top--; vis[ed[i].bh]=0;
// }
// return 0;
//}
void dfs(int x)
{
for(int i=hd[x];i;i=ed[i].nxt)
{
if(vis[ed[i].bh])continue;
vis[ed[i].bh]=; dfs(ed[i].to);
ans[++top]=ed[i].bh;
}
}
int main()
{
int x,y,z;
while()
{
scanf("%d%d",&x,&y);
if(!x&&!y)return ;
ct=; top=; tot=;
memset(hd,,sizeof hd);
memset(vis,,sizeof vis);
memset(deg,,sizeof deg);
for(int i=;i<=;i++) while(q[i].size())q[i].pop();
scanf("%d",&z); st=x;
insert(x,y,z);
while()
{
scanf("%d%d",&x,&y);
if(!x&&!y)break;
scanf("%d",&z);
insert(x,y,z);
}
add();
bool fl=;
for(int i=;i<=n;i++)
if(deg[i]%){printf("Round trip does not exist.\n"); fl=; break;}
if(fl)continue;
dfs(st); print();
// if(!dfs(st))printf("Round trip does not exist.\n");
}
}
poj1041 John's trip——字典序欧拉回路的更多相关文章
- UVA302 John's trip(欧拉回路)
UVA302 John's trip 欧拉回路 attention: 如果有多组解,按字典序输出. 起点为每组数据所给的第一条边的编号较小的路口 每次输出完额外换一行 保证连通性 每次输入数据结束后, ...
- POJ1041 John's trip
John's trip Language:Default John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- POJ1041 John's trip 【字典序输出欧拉回路】
题目链接:http://poj.org/problem?id=1041 题目大意:给出一个连通图,判断是否存在欧拉回路,若存在输出一条字典序最小的路径. 我的想法: 1.一开始我是用结构体记录边的起点 ...
- Java实现John's trip(约翰的小汽车)
1 问题描述 John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8998 Accepted: 3018 Sp ...
- John's trip(POJ1041+欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...
- poj 1041 John's trip——欧拉回路字典序输出
题目:http://poj.org/problem?id=1041 明明是欧拉回路字典序输出的模板. 优先队列存边有毒.写跪.学习学习TJ发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...
- 【poj1041】 John's trip
http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...
- poj 1041 John's trip 欧拉回路
题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. #include <iostream> #include < ...
- POJ 1041 John's trip 无向图的【欧拉回路】路径输出
欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为 ...
随机推荐
- html——a标签中target属性
有 4 个保留的目标名称用作特殊的文档重定向操作: _blank 浏览器总在一个新打开.未命名的窗口中载入目标文档. _self 这个目标的值对所有没有指定目标的 <a> 标签是默认目标, ...
- C# ADO.NET动态数据的增删改查(第五天)
一.插入登录框中用户输入的动态数据 /// <summary> /// 添加数据 /// </summary> /// <param name="sender& ...
- 【YOLO】实时对象检测使用体验
官网:https://pjreddie.com/darknet/yolo/ 以下全部在服务器上完成,服务器上是有opencv等. 1.安装Darknet git clone https://githu ...
- 【VHDL】组合逻辑电路和时序逻辑电路的区别
简单的说,组合电路,没有时钟:时序电路,有时钟. ↓ 也就是说,组合逻辑电路没有记忆功能,而时序电路具有记忆功能. ↓ 在VHDL语言中,不完整条件语句对他们二者的影响分别是什么?组合逻辑中可能生成锁 ...
- Lazarus Reading XML- with TXMLDocument and TDOMNode
这里读取'HistoryPath' ,'TracePath' 元素下的‘value’属性使用的是 var xmlCfg: TXMLDocument; .... function ReadXMLCFG: ...
- css 样式 解释
字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...
- HDU - 2159 FATE(二维dp之01背包问题)
题目: 思路: 二维dp,完全背包,状态转移方程dp[i][z] = max(dp[i][z], dp[i-1][z-a[j]]+b[j]),dp[i][z]表示在杀i个怪,消耗z个容忍度的情况下 ...
- JAVA学习总结-基础语法
/** * 这篇文章供自己学习JAVA总结回顾使用 * 主要借鉴了马士兵老师的视频进行总结 * @author Kingram */ 标识符的概念和命名规则 JAVA常量---不可变的变量 程序的执行 ...
- 16.copy_to定制组合field解决cross-fields搜索弊端
主要知识点: 在index的mapping中加copy_to字段的方法 copy_to搜索方法 用most_fields策略,去实现cross-fields搜索,有3大弊端,为了解决这三个弊端 ...
- 开启mysql远程连接
mysql默认只允许本地连接,也就是说,在安装完mysql后会存在两个root账户,他们的host分别是localhost和127.0.0.1 use mysql; update user set h ...