题目: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. Switch组件

    Switch组件,业务需求中经常遇到.我司的旧项目中,由于没有使用较为成熟点的组件库.自己实现了一个switch组件,但是实现的略微有些丑陋. 实现基本需求 https://jsfiddle.net/ ...

  2. vs2015 配置 cplex

    首先设置模式为Release, 根据软件选择x86或x64 附加库目录(链接器 - 常规) C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\lib\x6 ...

  3. PAT 1096. Consecutive Factors

    Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...

  4. springcloud(五):Eureka提供数据的客户端连接Docker的mysql

    一.提供数据的客户端需要连接数据了,因此需要我们使用mybatis了,等下使用idea生成mybaits和web的依赖 二.提供数据的客户端项目 1.创建项目 2.选择idea自动给我们生成的依赖 3 ...

  5. GeoTrust 企业(OV)型 多域名(SAN/UC)版 SSL证书

     GeoTrust 企业(OV)型 多域名(SAN/UC)版 SSL证书(GeoTrust True BusinessID With Multi-Domain(SAN/UC) ),支持多域名,属于企业 ...

  6. Codeforces Round #413(Div. 1 + Div. 2, combined)——ABCD

    题目在这里 A.Carrot Cakes 乱七八糟算出两个时间比较一下就行了 又臭又长仅供参考 #include <bits/stdc++.h> #define rep(i, j, k) ...

  7. JPA学习(基于hibernate)

    参考博客:https://blog.csdn.net/baidu_37107022/article/details/76572195 常用注解: https://blog.csdn.net/eastl ...

  8. ansible使用jinja2管理配置文件以及jinja2语法简介

    一.Jinja2介绍 Jinja2是基于python的模板引擎,功能比较类似于PHP的smarty,J2ee的Freemarker和velocity.它能完全支持unicode,并具有集成的沙箱执行环 ...

  9. 重庆OI2017 小 Q 的棋盘

    小 Q 的棋盘 时间限制: 1 Sec  内存限制: 512 MB 题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移 ...

  10. noip模拟赛 黑骑士

    题目描述江爷爷给你出了一道题:给你一个图,保证每个点最多属于一个简单环,每个点度数最多为3,求这个图有多少“眼镜图形个数”保证图联通哦~其中“眼镜图形个数”,定义为三元组(x,y,S),其中x和y表示 ...