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.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为 ...
随机推荐
- Python精要参考(第二版)
ython 精要参考(第二版) 是Python语言初学者不错的参考学习用书,本系列译自Python Essential Reference, Second Edition 希望本系列可以给python ...
- 【神仙题】【CF28D】 Don't fear, DravDe is kind
传送门 Description 一个有N辆卡车的车队从城市Z驶向城市3,来到了一条叫做"恐惧隧道"的隧道.在卡车司机中,有传言说怪物DravDe在那条隧道里搜寻司机.有些司机害怕先 ...
- 《剑指offer》— JavaScript(6)旋转数组的最小数字
旋转数组的最小数字 题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2, ...
- selenium - Select类 - 下拉框
WebDriver提供了Select类来处理下拉框. 如百度搜索设置的下拉框,如下图: from selenium import webdriver from selenium.webdriver.s ...
- HashCode与Equals回顾
HashSet和HashMap一直都是JDK中最常用的两个类,HashSet要求不能存储相同的对象,HashMap要求不能存储相同的键. 那么Java运行时环境是如何判断HashSet中相同对象.Ha ...
- win7 64位环境下配置汇编环境和程序设计
下载dosbox,并解压安装 下载地址: http://pan.baidu.com/s/1eRJbJAq 默认安装到C:\Program Files (x86)\DOSBox-0.74 安装成功后,双 ...
- 庞老师集群.ziw
2017年2月17日, 星期五 庞老师集群 链接:http://pan.baidu.com/s/1mhSw2TE 密码:hzz4 更改子网IP,及网关: null
- PHP与Ajax
如何用PHP接收JSON格式数据 1.一般来说,我们直接用$_POST $_REQUEST $_GET这样的超全局变量接收就好了 <?php $obj_temp=$_POST['data']; ...
- How to reset XiaoMi bluetooth headphone Youth edition.
To reset the speaker 1. Long press the phone call button to shut off the speaker 2. Connect the char ...
- wepy开发小程序 大坑....本地调试ok,小程序上传体验版 组件出现问题
如果你碰到的上述问题(本地调试ok,小程序上传体验版 各种莫名其妙的问题-卡死-组件属性失效-$apply()不起作用) 您需要关闭 微信开发者工具中: 1.微信开发者工具-->项目--> ...