URAL 1137 Bus Routes(欧拉回路路径)

时间:2022-02-12 21:03:46

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:
URAL 1137 Bus Routes(欧拉回路路径)

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&period; Bus Routes(dfs)

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

  3. &lbrack;LeetCode&rsqb; 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. &lbrack;Swift&rsqb;LeetCode815&period; 公交路线 &vert; 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 欧拉回路(套圈法&plus;欧拉回路路径输出&plus;骚操作)

    题目链接: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. &lbrack;LeetCode&rsqb; 815&period; 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&period; 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. 【jQuery基础学习】08 编写自定义jQuery插件

    目的:虽然jQuery各种各样的功能已经很完善了,但是我们还是要学会自己去编写插件.这样我们可以去封装一些项目中经常用到的专属的代码,以便后期维护和提高开发效率. jQuery插件的类型: 封装对象方 ...

  2. Mysql 高负载排查思路

    Mysql 高负载排查思路 发现问题 top命令 查看服务器负载,发现 mysql竟然百分之两百的cpu,引起Mysql 负载这么高的原因,估计是索引问题和某些变态SQL语句. 排查思路 1. 确定高 ...

  3. poj 3761 Bubble Sort&lowbar;快速幂

    题意:问你冒泡排序第i次排序,一共排了多少次 套公式K!((K + 1) ^ (N - K) - K ^ (N - K)) #include <iostream> #include< ...

  4. C&plus;&plus;中引用用于结构

    正确 void change(test &target) { target.name = "aaa"; } 错误 void change(const test &t ...

  5. window&period;showModalDialog刷新父窗口和本窗口的方法及注意

    window.showModalDialog刷新父窗口和本窗口的方法及注意:   一.刷新父窗口的方法:    A.使用window.returnValue给父窗口传值,然后根据值判断是否刷新. 在w ...

  6. JDBC也就那么回事

    JDBC 一.JDBC概述 为什么要使用JDBC? JDBC:Java DataBase Connectivity,是SUN公司提供的一套操作数据库的标准规范(技术). JDBC与数据库驱动的关系:接 ...

  7. 二进制编译安装httpd服务

    systemctl stop httpd yum remove httpd-----------------------(在做之前 先删掉httpd) 安装编译环境 yum -y groupinsta ...

  8. python matplotlib quiver——画箭头、风场

    理解参考:https://blog.csdn.net/liuchengzimozigreat/article/details/84566650 以下实例 import numpy as np impo ...

  9. C语言--第四次作业

    作业要求一 (70分) 实践最简答的项目wordcount,必须完成其中的基本功能,若可以完成其他功能给予加分.完成后请将你的设计思路.主要代码写在本次作业博客里. 真的迷茫<(_ _)> ...

  10. jenkins连接提示错误urllib&period;error&period;HTTPError&colon; HTTP Error 403

    昨天在执行python连接Jenkins获取编译失败日志失败时,出现错误,具体报错如下,主要是在连接问题上的问题,做了一个请求 就提示错误 原因在于Jenkins的权限,或者访问页面的url需要进行登 ...