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.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为 ...
随机推荐
- 【PL/SQL】九九乘法口诀表
--输出屏幕信息 SET serveroutput ON; --打印口诀表 DECLARE V_NUMBER1 ); --外层循环变量 V_NUMBER2 ); --内层循环变量 BEGIN .. - ...
- C#连接Sqlite 出现:混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。的解决方案
C#连接Sqlite 出现: 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集.的解决方案 C#连接sqlite数据库代码 ...
- 正则表达式提取String子串
最近遇到了一个字符串处理的功能,忽然发现了正则表达式的强大,深深的被她的迷人魅力所吸引,以前只是听说,今天亲眼所见,亲身经历,真的彻底折服. 言归正传:java中String类里面封装了很多字符串处理 ...
- Codeforces_733C
C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...
- java_第一个servlet小程序
xml中注册: <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>s ...
- Memcached 之PHP实现服务器集群一致性hash算法
/** * memcached 一致性hash,分布式算法 * Class MemcacheCluster */ class MemcacheCluster { protected $nodes = ...
- Centos7搭建ansible运维自动化工具
1)设置主机名和hosts文件 2)配置阿里云repo源 Wget -O /etc/yum.repos.d/aliyun.repo https://mirrors.aliyun.com/repo/Ce ...
- Linux - redis-cluster搭建
目录 Linux - redis-cluster搭建 Linux - redis-cluster搭建 1.准备6个数据库节点,也就是6个redis实例,也就是6个配置文件 配置文件如下 redis-7 ...
- C#关键字详解第三节
byte:字节 字节是计算机信息技术用于计量存储容量的一种计量单位,通常情况下一字节等于八位,也在一些计算机编程 语言中表示数据类型和语言字符.这是百度百科给出的解释,在C#语言中byte也可以是一种 ...
- HyperLedger项目以及社区
本文不涉及任何技术开发的内容,仅供你跟同学.同事吹牛B之用.就像很多牛人总爱讲历史典故一样. 一.诞生与现状 HyperLedger 诞生于2015年12月17日,HyperLedger 追寻Apac ...