poj 1041(字典序输出欧拉回路)
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 8641 | Accepted: 2893 | Special Judge | ||
Description
The streets in Johnny's town were named by integer numbers from 1 to
n, n < 1995. The junctions were independently named by integer
numbers from 1 to m, m <= 44. No junction connects more than 44
streets. All junctions in the town had different numbers. Each street
was connecting exactly two junctions. No two streets in the town had the
same number. He immediately started to plan his round trip. If there
was more than one such round trip, he would have chosen the one which,
when written down as a sequence of street numbers is lexicographically
the smallest. But Johnny was not able to find even one such round trip.
Help Johnny and write a program which finds the desired shortest
round trip. If the round trip does not exist the program should write a
message. Assume that Johnny lives at the junction ending the street
appears first in the input with smaller number. All streets in the town
are two way. There exists a way from each street to another street in
the town. The streets in the town are very narrow and there is no
possibility to turn back the car once he is in the street
Input
file consists of several blocks. Each block describes one town. Each
line in the block contains three integers x; y; z, where x > 0 and y
> 0 are the numbers of junctions which are connected by the street
number z. The end of the block is marked by the line containing x = y =
0. At the end of the input file there is an empty block, x = y = 0.
Output
one line of each block contains the sequence of street numbers (single
members of the sequence are separated by space) describing Johnny's
round trip. If the round trip cannot be found the corresponding output
block contains the message "Round trip does not exist."
Sample Input
1 2 1
2 3 2
3 1 6
1 2 5
2 3 3
3 1 4
0 0
1 2 1
2 3 2
1 3 3
2 4 4
0 0
0 0
Sample Output
1 2 3 5 4 6
Round trip does not exist. 题意:小明要从自己家走遍每一条街道有且仅只有一次访问每一个朋友然后回到自己家,这些路径都有一个权值,按照这些路径的字典序输出走法. 题解:很明显的欧拉回路,首先判断是否为欧拉回路,即每个点的度都是偶数,如果满足欧拉回路,先对每个结点所连接的边按照边的编号排序,这里利用 vector 中的 pair是最好不过了.然后进行一次深搜就行了.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
const int N = ;
vector < pair<int,int> > edge[N];
bool vis[N]; ///标记哪些边被访问过了
int in[N];
int out[N],ans[N],cnt;
void init()
{
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(vis,false,sizeof(vis));
for(int i=;i<N;i++) edge[i].clear();
cnt = ;
}
void addEdge(int u,int v,int w){
edge[u].push_back(make_pair(w,v));
edge[v].push_back(make_pair(w,u));
}
void dfs(int u){
for(int i=;i<edge[u].size();i++){
int e = edge[u][i].first;
int v = edge[u][i].second;
if(!vis[e]){
vis[e] = true;
dfs(v);
ans[cnt++] = e;
}
}
}
bool cmp(pair<int,int> a,pair<int,int> b){
return a.first<b.first;
}
int main()
{
int x,y,z,m;
while(scanf("%d%d",&x,&y)!=EOF)
{
int MIN = N,m=;
if(x==&&y==) break;
scanf("%d",&z);
init();
in[x]++,out[x]++;
in[y]++,out[y]++;
addEdge(x,y,z);
MIN = min(MIN,min(x,y));
while(scanf("%d%d",&x,&y)!=EOF)
{
if(x==&&y==) break;
scanf("%d",&z);
in[x]++,out[x]++;
in[y]++,out[y]++;
addEdge(x,y,z);
MIN = min(MIN,min(x,y));
}
bool flag = false;
for(int i=;i<N;i++){
if(in[i]%==||out[i]%==){
flag = true;
break;
}
if(edge[i].size()) sort(edge[i].begin(),edge[i].end(),cmp);
}
if(flag) {
printf("Round trip does not exist.\n");
continue;
}
dfs(MIN);
for(int i=cnt-;i>=;i--){
printf("%d ",ans[i]);
}
printf("\n");
}
}
poj 1041(字典序输出欧拉回路)的更多相关文章
- POJ1041 John's trip 【字典序输出欧拉回路】
题目链接:http://poj.org/problem?id=1041 题目大意:给出一个连通图,判断是否存在欧拉回路,若存在输出一条字典序最小的路径. 我的想法: 1.一开始我是用结构体记录边的起点 ...
- poj 1041 John's trip——欧拉回路字典序输出
题目:http://poj.org/problem?id=1041 明明是欧拉回路字典序输出的模板. 优先队列存边有毒.写跪.学习学习TJ发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...
- poj 2337 有向图输出欧拉路径
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10186 Accepted: 2650 Descrip ...
- ZOJ 3204 Connect them(字典序输出)
主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...
- 二叉排序树:HUD3999-The order of a Tree(二叉排序树字典序输出)
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- UVA 796 - Critical Links 无向图字典序输出桥
题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...
- 拓扑排序详解(梅开二度之dfs版按字典序输出拓扑路径+dfs版输出全部拓扑路径
什么是拓扑排序? 先穿袜子再穿鞋,先当孙子再当爷.这就是拓扑排序! 拓扑排序说白了其实不太算是一种排序算法,但又像是一种排序(我是不是说了个废话qwq) 他其实是一个有向无环图(DAG, Direct ...
- poj 1041(欧拉回路+输出字典序最小路径)
题目链接:http://poj.org/problem?id=1041 思路:懒得写了,直接copy吧:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路.一个无向图存 ...
- POJ 1041 John's trip 无向图的【欧拉回路】路径输出
欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为 ...
随机推荐
- hive1.1.0建立外部表关联HDFS文件
0. 说明 已经安装好Hadoop和hive环境,hive把元数据存储在mysql数据库.这里仅讨论外部表和HDFS的关联,并且删掉外部表之后,对HDFS上的文件没有影响. 1. 在HDFS创建分区, ...
- 【bzoj2006】超级钢琴
Portal --> bzoj2006 Solution 一开始看错题了..没有看到编号连续然后愣了好久== 首先肯定是找最大的\(K\)个啦,然后具体怎么找的话..没有什么特别好的办法那就 ...
- 【bzoj3589】动态树
Portal --> bzoj3589 Description 给你一棵\(n\)个节点的树,总共有\(q\)次操作,每次操作是以下两种中的一种: 操作\((0,x,delta)\):给以\(x ...
- Java 8十个lambda表达式案例【转】
1. 实现Runnable线程案例 使用() -> {} 替代匿名类: //Before Java 8: new Thread(new Runnable() { @Override public ...
- Spring MVC 向前台页面传值-ModelAndView
ModelAndView 该对象中包含了一个model属性和一个view属性 model:其实是一个ModelMap类型.其实ModelMap是一个LinkedHashMap的子类 view:包含了一 ...
- 报Cannot find /usr/local/tomcat/bin/setclasspath.sh错误
错误如下: [root@RSP-DEVWEB03 bin]#sh startup.sh Cannot find /usr/local/tomcat8081/bin/setclasspath.sh Th ...
- react-native安装react-navigation后出现package-lock.json文件的坑
npm5.0开始安装后回生成一个新的package-lock.json文件.以致初始化好的react-native项目引入的依赖被删除. 目前解决办法.使用facebook的yarn add 第三方组 ...
- 线上Redis偶发性链接失败排查记
问题过程 输入法业务于12月12日上线了词库接受业务,对部分用户根据用户uuid判断进行回传,在12月17日早上8点多开始出现大量的php报错(Redis went away),报错导致了大量的链接积 ...
- /etc/rc.d/里文件的作用
rc.sysinit指的是系统启动不管进哪个运行级别必须做的初始化工作, rcn.d目录指的是系统进对应n的运行级别时候系统必须做的工作,目录下S打头的服务指进此运行级别时候启动的服务,而K打头的指离 ...
- CF821 B. Okabe and Banana Trees 简单数学
Link 题意:给出一条直线,在直线上取一点,其垂直x,y轴作成一个,求矩阵中所有包含的点的x,y坐标之和的最大值. 思路:对于一个任意一点我们计算公式,对于任意一点$(x, y)$,有$(x+y)^ ...