URAL 1137Bus Routes (dfs)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Input
The number of stops: 1 ≤
m ≤ 1000.
The number-identifier of the stop: 1 ≤
l ≤ 10000.
Output
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 |
15 2 5 4 2 3 6 5 7 4 1 2 1 4 7 5 2 |
Notes
#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)的更多相关文章
- URAL 1242 Werewolf(DFS)
Werewolf Time limit: 1.0 secondMemory limit: 64 MB Knife. Moonlit night. Rotten stump with a short b ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
随机推荐
- Bot Framework:Activity类简明指南
Bot Framework相关文档:https://docs.botframework.com/en-us/csharp/builder/sdkreference/attachments.html B ...
- MySQL安装未响应解决方法
安装MySQL出示未响应,一般显示在安装MySQL程序最后2步的3,4项就不动了. 这种情况一般是你以前安装过MySQL数据库服务项被占用了. 1.卸载MySQL 2.删除安装目录及数据存放目录 3. ...
- UVALive 5031 Graph and Queries (Treap)
删除边的操作不容易实现,那么就先离线然后逆序来做. 逆序就变成了合并,用并存集判断连通,用Treap树来维护一个连通分量里的名次. Treap = Tree + Heap.用一个随机的优先级来平衡搜索 ...
- uva806 Spatial Structures 空间结构 (黑白图像的四分树表示)
input 8 00000000 00000000 00001111 00001111 00011111 00111111 00111100 00111000 -8 9 14 17 22 23 44 ...
- 在axios中使用async await
最近在做树鱼的项目, 联想到 如果用 async await 怎么处理, export async function Test1() { return new Promise((resolve) =& ...
- mini_httpd的安装和配置
1.下载:http://www.acme.com/software/mini_httpd/mini_httpd-1.19.tar.gz 2.解压:tar zxvf mini_httpd-1.19.ta ...
- 遍历Map的两种方式
取出map集合中所有元素的方式一:keySet()方法. 可以将map集合中的键都取出存放到set集合中.对set集合进行迭代.迭代完成,再通过get方法对获取到的键进行值的获取. Set keySe ...
- 配置centos7解决 docker Failed to get D-Bus connection 报错
在centos7的容器里面出现了一个BUG,就是serveice启动服务的时候出现报错,不能用service启动服务.[root@e13c3d3802d0 /]# service httpd star ...
- C语言程序运行
vs2013编辑器 c程序的运行 一.启动Microsoft Visual C++ 2013版.新建项目 . 1. 文件——> 新建——> 项目. 2. 确定之后 弹出 ...
- LeetCode(123) Best Time to Buy and Sell Stock III
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...