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.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为 ...
随机推荐
- jQuery——切换toggle
toggle()-----显示隐藏 toggleClass-------添加类或者删除类 <!DOCTYPE html> <html lang="en"> ...
- JAVA趣味逻辑算法
/**已知4位同学中的一位数学考了100分,当小李询问这4位是谁考了100分时,4个人的回答如下: A说:不是我. B说:是C C说:是D. D说:他胡说. 已知三个人说的是真话,一个人说的是假话.现 ...
- THREE.DecalGeometry(转载)
function getDecalGeometry(position, direction){ var decalGeometry = new THREE.DecalGeometry( earthMe ...
- if({1,0}, , )
=VLOOKUP(F2,IF({1,0},D2:D10,C2:C10),2,),用F2作为查找条件,对D列进行查找,如果改成{0,1}则是对C列为查找范围.返回内存数组,对F2进行精确查找. 为什么写 ...
- git怎么克隆远程仓库到本地仓库
参考: https://blog.csdn.net/zhangzeshan/article/details/81564990 不知道为什么输入git的克隆地址就会提示密码错误 ,使用http地址就直接 ...
- 兼容ie6的mvvm框架--san
最近,由于公司项目需要,使用百度mvvm框架san开发了一个兼容ie6的小项目.san的优势是强大的兼容性,能兼容到ie6.当初我自己好奇,尝试用san做了一个小型项目,这里记录一下.如果你也想尝试一 ...
- 模板中tempname与class区别
前言 在分析traits编程之前, 我们需要对模板参数类型tempname和class有一定的了解, 要明白他们在哪些方面不同, 哪些方面相同, 这样才能对体会到traits编程的核心. 如果你已经明 ...
- Luogu 2951 捉迷藏Hide and Seek
P2951 [USACO09OPEN]捉迷藏Hide and Seek 题目描述 Bessie is playing hide and seek (a game in which a number o ...
- _initialize() 区别 __construct()
_initialize()方法是在任何方法执行之前,都要执行的,当然也包括 __construct构造函数. 也就是说如果存在_initialize()函数,调用对象的任何方法都会导致_initial ...
- GlobalSign 域名型 SSL 证书
GlobalSign 域名型 SSL 证书,支持通配符型,只验证域名所有权,属于DV 域名验证级SSL证书,无须递交书面审查资料,网上申请便捷有效率.提供40位/56位/128位,最高256位自适 ...