题目大意:

输入 p,n,t ;p为地点数 判断 t 能否回到源点1

接下来n行 每行输入 a b c; a能到达b和c

Sample Input

13 6 7
6 7 8
2 3 4
10 11 12
8 9 10
1 2 13
4 5 6

Sample Output

5
1
2
4
6
7

 
可用深搜做
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int p,n,t;
int first[],nexti[],flag[];
int u[],v[],ans[],len;
bool DFS(int i,int cnt)
{
if(i==t&&cnt<len)
{
len=cnt;
ans[cnt]=i;
return cnt;
}
if(cnt>len) return ;
int k=first[i],sign=;
while(k!=-)
{
if(!flag[v[k]])
{
flag[v[k]]=;
if(DFS(v[k],cnt+)!=)
ans[cnt]=i, sign=;
flag[v[k]]=;
}
k=nexti[k];
}
if(sign) return cnt;
else return ;
}
int main()
{
while(~scanf("%d%d%d",&p,&n,&t))
{
memset(first,-,sizeof(first));
memset(flag,,sizeof(flag));
for(int i=;i<=n*;i++)
{
scanf("%d%d",&u[i],&v[i]);
nexti[i]=first[u[i]];
first[u[i]]=i++;
u[i]=u[i-]; scanf("%d",&v[i]);
nexti[i]=first[u[i]];
first[u[i]]=i;
}
flag[]=;
len=INF;
DFS(,);
printf("%d\n",len);
for(int i=;i<=len;i++)
printf("%d\n",ans[i]);
} return ;
}

邻接表深搜

但用并查集更简单 还是简化的

#include<stdio.h>
int root[];
int get(int cnt,int m)
{
if(m==)
{
printf("%d\n",cnt);
return ;
}
if(get(cnt+,root[m]))
printf("%d\n",root[m]);
}
int main()
{
int p,n,t;
while(~scanf("%d%d%d",&p,&n,&t))
{
for(int i=;i<=p;i++) root[i]=i;
while(n--)
{
int a,b,c; scanf("%d%d%d",&a,&b,&c);
root[b]=root[c]=a;
}
if(get(,t)) printf("%d\n",t);
} return ;
}

并查集

USACO 2009 Open Treasure Cave /// DFS||并查集 oj26215的更多相关文章

  1. 分珠(dfs+并查集)

    1140 分珠 时间限制:500MS  内存限制:65536K提交次数:24 通过次数:18 题型: 编程题   语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不 ...

  2. Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)

    <题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...

  3. CodeForces - 455C Civilization (dfs+并查集)

    http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...

  4. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  5. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

  6. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  7. CF 115 A 【求树最大深度/DFS/并查集】

    CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outp ...

  8. 51nod1307(暴力树剖/二分&dfs/并查集)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...

  9. ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

    Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB    ...

随机推荐

  1. FreeMarker简单入门到使用

    FreeMarker freemarker是一个用java开发的模版引擎,百度百科: 常用的java模版还有快要被抛弃的Jsp(熟悉).Thymeleaf(了解).Velocity(不知) freem ...

  2. Django使用步骤

    pip install django django-admin startproject mysite tree django-admin startapp mysite_user django-ad ...

  3. Delphi ADOQuery

    Delphi ADOQuery procedure TForm1.Button1Click(Sender: TObject); var A: Array of String;//定义动态数组 Inde ...

  4. AtCoder ABC 128E Roadwork

    题目链接:https://atcoder.jp/contests/abc128/tasks/abc128_e 题目大意 在一条路上(这条路可以看做坐标轴 x 轴从 0 开始方向为正无穷方向的射线),有 ...

  5. 解决jqGrid中,当前页一直显示为0的问题

    项目中,经常会见到使用 jqGrid 进行一些数据的列表展示,而且使用起来也比较方便.但是有时会遇到一些奇怪的问题,比如前几天我就遇到了在使用 jqGrid 时,当前页一直显示为 0 的问题.下图就是 ...

  6. VS新建工程或者新建项时 出现未定义标识符号

    VS新建工程或者新建项时 出现未定义标识符号,编译之后不影响运行,但是看着很不舒服,影响效率. 解决办法:属性--->VC++目录-->包含目录-->编辑,将自己所用QT的inclu ...

  7. 初探分布式环境的指挥官ZooKeeper

    目录 1. 从单机到集群,分布式环境中的挑战 1.1 集中式的特点 1.2 集中式的痛点 1.3 从单体到SOA的转变 1.4 分布式服务总体框架 1.5 分布式应用概述 2. ZK基本概念及核心原理 ...

  8. java执行spark查询hbase的jar包出现错误提示:ob aborted due to stage failure: Master removed our application: FAILED

    执行java调用scala 打包后的jar时候出现异常 /14 23:57:08 WARN TaskSchedulerImpl: Initial job has not accepted any re ...

  9. spark2.+ sql 性能调优

    1.在内存中缓存数据 性能调优主要是将数据放入内存中操作,spark缓存注册表的方法 版本 缓存 释放缓存 spark2.+ spark.catalog.cacheTable("tableN ...

  10. Codeforces 479【A】div3试个水

    题目链接:http://codeforces.com/problemset/problem/977/A 题意:这个题,题目就是让你根据他的规律玩嘛.末尾是0就除10,不是就-1. 题解:题解即题意. ...