题目: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——字典序欧拉回路的更多相关文章

  1. UVA302 John's trip(欧拉回路)

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

  2. POJ1041 John's trip

    John's trip Language:Default John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  3. POJ1041 John's trip 【字典序输出欧拉回路】

    题目链接:http://poj.org/problem?id=1041 题目大意:给出一个连通图,判断是否存在欧拉回路,若存在输出一条字典序最小的路径. 我的想法: 1.一开始我是用结构体记录边的起点 ...

  4. Java实现John's trip(约翰的小汽车)

    1 问题描述 John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8998 Accepted: 3018 Sp ...

  5. John's trip(POJ1041+欧拉回路+打印路径)

    题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...

  6. poj 1041 John's trip——欧拉回路字典序输出

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

  7. 【poj1041】 John's trip

    http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...

  8. poj 1041 John's trip 欧拉回路

    题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. #include <iostream> #include < ...

  9. POJ 1041 John's trip 无向图的【欧拉回路】路径输出

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

随机推荐

  1. which

    功能说明:显示命令的全路径.   参数选项: -a  默认在PATH路径中由前往后查找命令,如果查找到了,就停止匹配.使用-a选项将遍历所有PATH路径,输出所有匹配项.   参数-a把所有匹配命令路 ...

  2. .mm c++ oc 混编

    When you create a static library you don't link in the dependent libraries. As a result, when you re ...

  3. Spring Boot项目中使用 TrueLicense 生成和验证License(服务器许可)

    一 简介 License,即版权许可证,一般用于收费软件给付费用户提供的访问许可证明.根据应用部署位置的不同,一般可以分为以下两种情况讨论: 应用部署在开发者自己的云服务器上.这种情况下用户通过账号登 ...

  4. Requirejs常用配置和应用

    requirejs.require方法冲突 如果加载了多个requirejs脚本,每个requirejs会判断是否浏览器已经实现了require和define方法.如果浏览器已经自带require和d ...

  5. 用 ilasm 反编译、修改.net dll文件

    有些.net dll我们没有源码,如果要修改某些东西,可以用ilasm.exe反编译为il代码,修改后再编译回dll ilasm通常放在以下路径 C:\Windows\Microsoft.NET\Fr ...

  6. logback日志配置文件

    application.properties application.properties logback-spring.xml <?xml version="1.0" en ...

  7. C# 后台POST提交方式

    1.第一种方式:用最新框架,但是针对IIS服务器的操作系统有关系,非R2的收不到数据: using (var reqConts = new MultipartFormDataContent()) { ...

  8. vue中的父组件及子组件生命周期的执行顺序

    一.没有任何任何显示与隐藏限制条件的情况下: 1.运行的顺序依次是: 父组件created→父组件beforeMounted→子组件created→子组件beforeMounted→子组件mounte ...

  9. 洛谷——P1896 [SCOI2005]互不侵犯

    P1896 [SCOI2005]互不侵犯 状压DP入门题 状压DP一般需要与处理状态是否合法,节省时间 设定状态dp[i][j][k]表示第i行第j个状态选择国王数为k的方案数 $dp[i][j][n ...

  10. Python面向对象----继承, 重载

    1. 面向对象三大特性之继承. 继承的便捷是子类可以直接调用父类里面的方法和属性.(在强类型语言里面是只能调用公有成员), 不用重复的造轮子. 减少程序猿的负担.实现多态等上层结构 2. 父类代码 3 ...