URAL 1137 Bus Routes(欧拉回路路径)
1137. Bus Routes
Memory limit: 64 MB
bus routes were in the city of Fishburg. None of the routes shared the
same section of road, though common stops and intersections were
possible. Fishburg old residents stated that it was possible to move
from any stop to any other stop (probably making several transfers). The
new mayor of the city decided to reform the city transportation system.
He offered that there would be only one route going through all the
sections where buses moved in the past. The direction of movement along
the sections must be the same and no additional sections should be used.
Input
and the list of that stops. Bus stops are identified by positive
integers not exceeding 10000. A route is represented as a sequence of m + 1 bus stop identifiers: l1, l2, …, lm, lm+1 = l1
that are sequentially visited by a bus moving along this route. A route
may be self-intersected. A route always ends at the same stop where it
starts (all the routes are circular).
The number of stops: 1 ≤ m ≤ 1000.
The number-identifier of the stop: 1 ≤ l ≤ 10000.
Output
stop must be the same as the first. If it is impossible to make a new
route according to the problem statement then write 0 (zero) to the
output.
Sample
| input | output |
|---|---|
3 |
15 2 5 4 2 3 6 5 7 4 1 2 1 4 7 5 2 |
Notes
Problem Source: Quarterfinal, Central region of Russia, Rybinsk, October 17-18 2001
【分析】给出一些公交站之间的路(可认为单向),然后让你设计一条回路,包含所有已有的路。
有向图欧拉回路并输出路径,可用Fleury(弗罗莱)算法。下面是模板。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int n,m,cnt=;
int tot=,s,t;
int head[N],dis[N],vis[N][N],pre[N];
int in[N],out[N];
stack<int>st;
struct man {
int to,next;
} edg[N];
void add(int u,int v) {
in[v]++;out[u]++;
edg[tot].to=v;
edg[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u){
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(!vis[u][v]){
vis[u][v]=;
dfs(v);
}
}
st.push(u);
}
int main() {
int u,v,nn=,sum=;
met(head,-);
scanf("%d",&n);
while(n--){
scanf("%d%d",&m,&u);nn=max(nn,u);sum+=m;
while(m--){
scanf("%d",&v);nn=max(nn,v);
add(u,v);u=v;
}nn+=m;
}
int num=,start;
vector<int>vec;
for(int i=;i<=nn;i++){
if(in[i]!=out[i])num++,vec.push_back(in[i]-out[i]);
}
if(num!=||(num==&&vec[]!=-&&vec[]!=)||(num==&&vec[]!=&&vec[]!=-))puts();
dfs();
printf("%d",sum);
while(!st.empty()){
int u=st.top();
st.pop();
printf(" %d",u);
}printf("\n");
return ;
}
URAL 1137 Bus Routes(欧拉回路路径)的更多相关文章
- URAL 1176 Hyperchannels(欧拉回路路径)
Hyperchannels Time limit: 1.0 secondMemory limit: 64 MB The Galaxy Empire consists of N planets. Hyp ...
- 1137. Bus Routes(dfs)
1137 做过一样的 怎么又忘了 再一次搜超时 不用回溯 #include <iostream> #include<cstdio> #include<cstring> ...
- [LeetCode] Bus Routes 公交线路
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...
- [Swift]LeetCode815. 公交路线 | Bus Routes
We have a list of bus routes. Each routes[i]is a bus route that the i-th bus repeats forever. For ex ...
- UOJ 117 欧拉回路(套圈法+欧拉回路路径输出+骚操作)
题目链接:http://uoj.ac/problem/117 题目大意: 解题思路:先判断度数: 若G为有向图,欧拉回路的点的出度等于入度. 若G为无向图,欧拉回路的点的度数位偶数. 然后判断连通性, ...
- LeetCode解题报告—— Bus Routes
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...
- [LeetCode] 815. Bus Routes 公交路线
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...
- 【leetcode】815. Bus Routes
题目如下: We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. ...
- hdu 5552 Bus Routes
hdu 5552 Bus Routes 考虑有环的图不方便,可以考虑无环连通图的数量,然后用连通图的数量减去就好了. 无环连通图的个数就是树的个数,又 prufer 序我们知道是 $ n^{n-2} ...
随机推荐
- win10环境下使用苹果虚拟机不要开多线程应用下载文件
win10环境下使用苹果虚拟机开多线程应用下载文件时候卡死,网络老掉. 8GB内存不够用?2.5mb网速不够用? 开的百度网盘下载个电影 结果虚拟机卡的不行 关了 网盘 挂起虚拟机 然后再 继续运行客 ...
- Portlet和servlet的区别
相同之处 l 都是java技术开发的web组件 l 都是由特定的容器在管理 l 都可以动态产生各种内容 l 生命周期都是由容器管理 l 和客户端的交互通过request/response机制 不同之处 ...
- sql 给数据库表 字段 添加注释
最近发现一些代码生成器 有针对注释做一个很好的转化,之前建表的时候 没有这块的注释.现在想增加,专门去看了下 如何增加注释 1 -- 表加注释 2 EXEC sys.sp_addextendedpro ...
- JAVA每日一旅3
1.关于byte byte在内存中占一个字节,范围是-128-127,128作强制类型转换到byte变成-128,因为128的二进制表示:1000 0000,最高位是符号位. 2.关于Hibernat ...
- SharePoint 2013 搜索体系结构
博客地址:http://blog.csdn.net/FoxDave 本文参考自微软官方的Chart,记录一下,算是自己对这部分知识的总结. Microsoft® SharePoint® Server ...
- 数组的foreach方法和jQuery中的each方法
/* * 数组的forEach方法: * 1.返回给回调的参数先是值,然后是下标 * 2.回调函数执行时内部的this指向window * */ /*var arr = [1,2,3,4,5]; ar ...
- iOS上架(转)
自己的经验总结,有错的话请留言,第一时间更改. 先大概说一下IOSAPP上架的几个步骤(详细步骤见下图): 创建证书请求文件 登录苹果开发者中心生成发布者证书(下载下来要双击一下) 设置APPID(要 ...
- phpunit+selenium环境搭建
这个环境搭建遇到了挺多麻烦,最终还是没能自己解决,幸好有同事“青蛙”的帮忙解决了这个问题!在这里把本人亲测步骤给大家列一下,希望给大家提供方便! 安装pear: Go-pear.phar下载地址:ht ...
- Note_Master-Detail Application(iOS template)_01_YJYAppDelegate.h
//YJYAppDelegate.h #import <UIKit/UIKit.h> @interface YJYAppDelegate : UIResponder <UIAppli ...
- tornado初步 ppt分享
组内的tornado分享,初步: http://files.cnblogs.com/files/yuhan-TB/tornado.pptx