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).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
随机推荐
- POJ 1651 Multiplication Puzzle (区间DP,经典)
题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...
- 使用脚本在Linux服务器上自动安装Kubernetes的包管理器Helm
Helm之于Kubernetes好比yum之于Red Hat Enterprise Linux,或者apt-get之于Ubuntu. Helm是由helm CLI和Tiller组成,是典型的Clien ...
- line-block,white-space,overflow
line-block:设置行间的距离(行高),只能控制块级元素,span这样的行内元素无法控制,并且当块级元素 中包含span的时候设置line-block会使span的自适应高度小于块级元素的高度, ...
- UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)
不难发现,每过一个小时,除了右下方的气球全都是蓝色以外,其他都和上一个小时的气球是一样的,所以是可以递推的.然后定义一类似个前缀和的东西f(k,i)表示k小时之后上面i行的红气球数.预处理出k小时的红 ...
- Service Unavailable HTTP Error 503. The service is unavailable.
原因: public void SetCurrentType(string[] projTypes) { _ProjTypes = _ProjTypes; } 确保没有无限递归或无限循环
- NodeJS基础API-path相关的问题basename,extname,dirname,parse,format,sep,delimiter,win32,posix
path 参考文档:http://nodejs.cn/api/path.html const {normalize} = require('path'); // ES6语法 // 相当于 const ...
- vue之列表循环
文档:https://cn.vuejs.org/v2/guide/list.html 当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用“就地复用”策略.如果数据项的顺序被改变, ...
- java的面向对象 (2013-09-30-163写的日志迁移
1)面向对象的特征 1. 抽象:(从java方面来说抽象大多数人还是把它作为java中的一种特征来对待) 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象包括 ...
- 19.Yii2.0框架模型删除记录
目录 //删除记录 //http://yii.com/?r=home/del public function actionDel() { //查出要删除的记录行 // 方法一:(查一行,删一行) // ...
- hessian应用示例
因为公司的项目远程调用采用的是hessian,故抽时间了解了下hessian,自己也写了一个应用实例,以便加深对hessian的理解. Hessian是一个轻量级的remoting onhttp工具, ...