Z - Bus Routes

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

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

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:
题意就是给你几条旧的路线,让你求能否设计一条新的路线,可以包含这里面的所有路线,dfs变换一下就可以了,但是注意一点就是再回塑的时候不要把标记vis即系清空
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define N 10010
int n,m;
vector<int>ed[N];
bool vis[N][N];
int pa[],t;
void dfs(int u){
int i;
for(i = ;i < (int)ed[u].size() ; i++){
int v = ed[u][i];
if(!vis[u][v]){
vis[u][v] = ;
dfs(v);
}
}
t++;
pa[t] = u;
return ;
}
int main(){
int i,j,u,v;
scanf("%d",&n);
int sum=;
for(i = ; i <= n ; i++){
scanf("%d",&m);
sum+=m;
scanf("%d",&u);
for(j = ; j <= m ; j++)
{
scanf("%d",&v);
ed[u].push_back(v);
u = v;
}
}
dfs();
if(sum!=t-)
printf("0\n");
else{
printf("%d\n",t-);
for(i = t; i > ; i--)
printf("%d ",pa[i]);
printf("%d\n",pa[]);
}
return ;
}

URAL 1137Bus Routes (dfs)的更多相关文章

  1. URAL 1242 Werewolf(DFS)

    Werewolf Time limit: 1.0 secondMemory limit: 64 MB Knife. Moonlit night. Rotten stump with a short b ...

  2. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  3. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  4. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  5. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  6. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  7. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  8. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  9. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

随机推荐

  1. JavaScript 的 parseInt 取整

    http://www.neoease.com/javascript-get-integer-via-parseint/ JavaScript 是弱类型语言, 为了保证数值的有效性, 在处理数值的时候, ...

  2. [Tracking] KCF + KalmanFilter目标跟踪

    基于KCF和MobileNet V2以及KalmanFilter的摄像头监测系统 简介 这是一次作业.Tracking这一块落后Detection很多年了,一般认为Detection做好了,那么只要能 ...

  3. spring maven 包

    <spring-framework.version>.RELEASE</spring-framework.version> <dependency> <gro ...

  4. 单核CPU并发与非并发测试

    多线程运行程序的目的一般是提高程序运行效率并且能够提高硬件的利用率比如多核CPU,但是如果我们只有单核CPU并发运行程序会怎样呢? 我以两个环境作为对比: 环境A(我本机8c) 环境B(我的云服务器1 ...

  5. 关于"动态语言" "静态语言" "静态类型语言" "动态类型语言"的区别

    参考链接:关于“编译型”“解释型”“动态语言”“静态语言”“动态类型语言”“静态类型语言”的区分以及优缺点(汇总整理) 很多人把这两类混为一谈,但是这是完全不同的两个概念!!! 动态和静态语言主要看的 ...

  6. MVC和MVP到底有什么区别呢?

    MVC和MVP到底有什么区别呢?   MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写 MVP 全称:Model-V ...

  7. 安装IAR ewarm

    一  安装准备 (ST方案) 1 嵌入式集成开发环境IAR ewarm 5.41 2 J-Link4.20 3 emberznet-4.3.0协议栈安装包 option1:tools - stm32软 ...

  8. iOS快速开发框架--Bee Framework

    Bee Framework是一款iOS快速开发框架,允许开发者使用Objective-C和XML/CSS来进行iPhone和iPad开发,由 Gavin Kwoe 和 QFish 开发并维护. 其早期 ...

  9. sql快速删除所用表,视图,存储过程

    [http://www.th7.cn/db/mssql/2011-07-07/10127.shtml#userconsent#] 删除用户表 .select 'DROP TABLE '+name fr ...

  10. linux文件或文件夹常见操作

    创建文件夹 mkdir [-p] DirName  在工作目录下,建立一个名为 A 新的子目录 : mkdir A  在工作目录下的 B目录中,建立一个名为 T 的子目录:    若 B 目录不存在, ...