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} ...
随机推荐
- xcode6 ios launchimage
1.点击Image.xcassets 进入图片管理,然后右击,弹出"New Launch Image" 2.右侧的勾选可以让你选择是否要对ipad,横屏,竖屏,以及低版本的ios系 ...
- QuartZ的线程锁
; ...
- Android Studio 配置JPush
1.在JPush官方下载 JPush SDK(jpush-android-arm-2.1.0.zip),我下载的是2.1.0: 2.解压下载好的压缩包(jpush-android-arm-2.1.0. ...
- JavaScript中Date的一些细节
对于开发人员来说,Date有时候或许会很重要,我们可以通过new Date()来创建一个日期对象.例如: var start = new Date(), //获取当前时间 today = new Da ...
- [网络技术]VPN设置
1.解决VPN服务器默认路由困扰 现在移动办公已经变得家常便饭,每次外出出差办公需要访问单位的内网服务器时,该怎么办呢?相信很多人都想到了VPN连接!的确,使用VPN连接, 我们可以利用现成的Inte ...
- JS原生回到顶部效果
// 回到顶部 onload = function () { var oBtnTop = document.getElementById('toTop'); var timer = null; oBt ...
- 查询某个表或者所有表的字段说明 SQLServer
查询某个表或者所有表的字段说明SELECT [Table Name] = OBJECT_NAME(c.object_id), [Column Name] = c.name, [Des ...
- Palindrome Number ---- LeetCode 009
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- swift SDWebImage使用
Web image(网络图像) 该库提供了一个支持来自Web的远程图像的UIImageView类别它提供了: 添加网络图像和缓存管理到Cocoa Touch framework的UIImageView ...
- 使用Office 365 试用账户 体验Office 365功能
首先,需要申请Office 365 Enterprise E3版本的试用账户. 在浏览器中,输入以下地址,然后回车: http://products.office.com/en-us/business ...