1137. Bus Routes

Time limit: 1.0 second
Memory limit: 64 MB
Several
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.
Write a program, which creates one of the possible new routes or finds out that it is impossible.

Input

The first line of the input contains the number of old routes n. Each of the following n lines contains the description of one route: the number of stops m
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 old routes: 1 ≤ n ≤ 100.
The number of stops: 1 ≤ m ≤ 1000.
The number-identifier of the stop: 1 ≤ l ≤ 10000.

Output

The output contains the number of stops in the new route k and the new route itself in the same format as in the input. The last (k+1)-th
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
6 1 2 5 7 5 2 1
4 1 4 7 4 1
5 2 3 6 5 4 2
15 2 5 4 2 3 6 5 7 4 1 2 1 4 7 5 2

Notes

Here is a picture for the example:

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(欧拉回路路径)的更多相关文章

  1. URAL 1176 Hyperchannels(欧拉回路路径)

    Hyperchannels Time limit: 1.0 secondMemory limit: 64 MB The Galaxy Empire consists of N planets. Hyp ...

  2. 1137. Bus Routes(dfs)

    1137 做过一样的 怎么又忘了 再一次搜超时 不用回溯 #include <iostream> #include<cstdio> #include<cstring> ...

  3. [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 ...

  4. [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 ...

  5. UOJ 117 欧拉回路(套圈法+欧拉回路路径输出+骚操作)

    题目链接:http://uoj.ac/problem/117 题目大意: 解题思路:先判断度数: 若G为有向图,欧拉回路的点的出度等于入度. 若G为无向图,欧拉回路的点的度数位偶数. 然后判断连通性, ...

  6. 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 ...

  7. [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 ...

  8. 【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. ...

  9. hdu 5552 Bus Routes

    hdu 5552 Bus Routes 考虑有环的图不方便,可以考虑无环连通图的数量,然后用连通图的数量减去就好了. 无环连通图的个数就是树的个数,又 prufer 序我们知道是 $ n^{n-2} ...

随机推荐

  1. HDU 3333 树状数组离线查询

    题目大意: 询问区间内不同种类的数的数值之和 这里逐个添加最后在线查询,会因为相同的数在区间内导致冲突 我们总是希望之后添加的数不会影响前面,那么我们就在添加到第i个数的时候,把所有在1~i 的区间的 ...

  2. C# TCP实现多个客户端与服务端 数据 与 文件的传输

    C#菜鸟做这个东东竟然花了快三天的时间了,真是菜,菜,菜--- 下面是我用C#写的 一个简单的TCP通信,主要的功能有: (1) 多个客户端与服务器间的数据交流 (2)可以实现群发的功能 (3)客户端 ...

  3. fragment的一些bug

    自从Android3.0引入了Fragment之后,使用Activity去嵌套一些Fragment的做法也变得更加流行,这确实是 Fragment带来的一些优点,比如说:Fragment可以使你能够将 ...

  4. 关于netbeans中的JComboBox(复选框)

    以最近写的选课系统中添加课程项为例 1.往复选框中放入选项(根据数据库添加) (1)首先将  属性—>model中默认Item1234清空 (2)获得数据库中的数据并放入. SelectCour ...

  5. 新浪微博sdk bug

    最近在做一个 iOS 的 cocos2d-x 项目接入新浪微博 SDK 的时候被“坑”了,最后终于顺利的解决了.发现网上也有不少人遇到一样的问题,但是能找到的数量有限的解决办法写得都不详细,很难让人理 ...

  6. cookie 内容的获取

    NSMutableArray *cookiesStoreAll = [[NSMutableArray alloc]init]; NSUInteger totalNumberOfCookies;     ...

  7. Git 的安装和创建版本库 。

    Git 的优点就不再多说了 .直接进入正题吧 . 安装Git 首先可以尝试输入 Git 看看有没有反映 . $ git The program 'git' is currently not insta ...

  8. PHP、C++的重载

    首先明确一点:PHP重载是用在面向对象的类当中,而不支持函数重载. 这点与C++不一样,在C++当中,重载可以用于面向过程和面向对象,而且方法也不一样. 在C++中,重载适用于当函数名相同时,函数所需 ...

  9. List<T>Contains, Exists, Any之间的优缺点对比

    在List<T>中,Contains, Exists, Any都可以实现判断元素是否存在. 性能方面:Contains 优于 Exists 优于 Any 测试的代码: public sta ...

  10. Java 集合深入理解(5):AbstractCollection

    点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 今天好累,来学学 AbstractCollection 吧! 什么是 AbstractCollection Abstrac ...