题目大意:

输入 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. Hbase和Hive在大数据架构中处在不同位置

    先放结论:Hbase和Hive在大数据架构中处在不同位置,Hbase主要解决实时数据查询问题,Hive主要解决数据处理和计算问题,一般是配合使用.一.区别:Hbase: Hadoop database ...

  2. Unity 中调用Android的JAVA代码

    首先我们要创建一个android项目 因为项目需要使用Unity提供的接口,所以需要将接口classes.jar引入至当前工程但中.接口包的所在地,打开Finder->应用程序->Unit ...

  3. prop不同数据类型设置默认值

    vue prop 会接收不同的数据类型,这里列出了 常用的数据类型的设置默认值的写法,其中包含: Number, String, Boolean, Array,  Function, Object   ...

  4. HDU 3966 /// 树链剖分+树状数组

    题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...

  5. Android笔记之Fragment中创建ViewModel的正确方式

    之前一直都是这么写的 pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class); //参数this是当前fragment ...

  6. 【POJ】3259 Wormholes

    题目链接:http://poj.org/problem?id=3259 题意:n个农场,m条双向路径,w条单向路径(虫洞).单向虫洞路径是负值.农夫想知道自己能不能看到自己(X). 题解:其实刚开始没 ...

  7. 2018今日头条湖北省赛【A】

    [题目链接]https://www.nowcoder.com/acm/contest/104/A 这题就是很简单的几何题..md现场推了很久的cos sin仿佛像个zz.自己都想给自己一巴掌. 题意就 ...

  8. Neo4j使用简单例子

    Neo4j Versions Most of the examples on this page are written with Neo4j 2.0 in mind, so they skip th ...

  9. Maven IntelliJ IDEA设置

    参考:博客地址: https://blog.csdn.net/huo920/article/details/82082403 Maven常用配置 在配置之前请将JDK安装好. 1. 环境变量配置 添加 ...

  10. SonarQube代码质量扫描持续集成

    1.安装JDK和配置JAVA_HOME和CLASSPATH 2.安装mysql数据库 3.创建数据库和用户 mysql -u root -p mysql> CREATE DATABASE son ...