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. Windows任务计划

    任务计划,可以将任何脚本.程序或文档安排在某个时间运行.“任务计划”在每次启动windows系统的时候自动启动(默认Task Scheduler服务是开启的)并在后台运行.使用“任务计划”可以完成以下 ...

  2. String字符串包含运算符实现运算

    string aa = "(1+2)/3+(3+4)*5"; DataTable dt = new DataTable(); string b = dt.Compute(aa, & ...

  3. (转载)重新对APK文件签名

    1.将证书(debug.keystore)复制到与需要重新签名的apk文件相同的目录下(如:复制到D:\Sign) 2.在cmd中切换到需要重新签名的apk文件的目录下 3.使用WinRAR打开要重新 ...

  4. VS2010下配置OCI编程

    OCI是Oracle官方出品的用于C/C++语言连接.操作Oracle数据库的API.在windows操作系统下使用VS等IDE编写.编译C++程序十分方便.简单,不需要使用Makefile.使用OC ...

  5. [安卓][转]Android eclipse中程序调试

    一:断点调试 用eclipse开发android程序的时,跟VS一样是可以断点单步调试的.步骤如下.1 设置断点:在编码窗体的左边框上用鼠标双击,或者右键点击菜单,选择 Toggle Breakpoi ...

  6. 如何使用SVN管理我们的源代码

    今天把公司的SVN服务器配置给做了一下,根据公司部门的不同,划分了不同的访问目录,并给不同目录配置了相应的权限,算是把这份差事给干完了,但其实我对自己的工作是不满意和有遗憾的,因为目前公司的SVN服务 ...

  7. hibernate实现增删改查的各种方法

    1>接口(主要是增删改查的接口)BaseDao.java /** * * @author fly.zhou */ public interface IBaseDao { //增加对应实体的一条记 ...

  8. [IE兼容性] Table 之边框 (IE6 IE7 IE8(Q) 中 cellspacing 属性在重合的边框模型的表格中仍然有效)

    在 IE6 IE7 IE8(Q) 中,在通过 border-collapse:collapse 使用表格的重合边框模型后,其 cellspacing 属性仍然有效: 在 其他浏览器 中,此时的 cel ...

  9. linux基础命令(二)用户管理和权限管理

  10. HID高级攻击姿势:利用PowerShell脚本进行文件窃取

    0×01 引言 又到了期中考试了,我又要去偷答案了,一直发现远程下载运行exe的方式不太好,容易报毒所以这里打算用ps脚本. 0×02 关于HID HID是Human Interface Device ...