题目大意:

输入 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. delphi xe10 手机程序事件服务操作、退出键操作

    //程序事件服务操作 var FMXApplicationEventService: IFMXApplicationEventService; begin if TPlatformServices.C ...

  2. Springboot文件上传限制

    #100,000,000 100M spring.servlet.multipart.max-file-size = 100000000 spring.servlet.multipart.max-re ...

  3. Redis缓存数据库常见操作

    Jedis的最为常见的操作.主要包括常用的列表(list).集合(set).有序集合(sorted set).哈希表(hash)等数据结构,以及其他特性支持. 参考资料:http://hello-ni ...

  4. 「题解」:y

    问题 B: y 时间限制: 1 Sec  内存限制: 256 MB 题面 题面谢绝公开. 题解 考虑双向搜索. 定义$cal_{i,j,k}$表示当前已经搜索状态中是否存在长度为i,终点为j,搜索过边 ...

  5. Feign连接超时 Read timed out

    在远程调用的过程中由于连接超时,导致无法成功请求数据,下面是报错 项目中用的是spring-cloud-starter-openfeign 2.2.0版本 找到对应的文档,开始查阅资料 文档首页就找到 ...

  6. C 函数指针详解

    一 通常的函数调用    一个通常的函数调用的例子://自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int a ...

  7. spring boot部署到阿里云碰到的总总问题

    2375错误,我没装docker,从pom中删了吧 mysql,不能写本机对外,得写127.0.0.1. 如何生成jar包,在pom中写上jar <groupId>com.coding&l ...

  8. GIT学习记录3(分支管理)

    学习参考地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 本编随笔只是自己对 ...

  9. Scala 可变长参数

    Scala 允许你指明函数的最后一个参数可以是重复的.这可以允许客户向函数传入可变长度参数列表.想要标注一个重复参数,在参数的类型之后放一个星号. 例如:定义一个可变参数的函数param def pa ...

  10. pd.Panel转化成json,然后再还原回来

    在使用tornado的write时候有一个需求,是将panel转化成json;而接收端再将json还原成panel格式. 尝试了很久,终于实现了. panel1 =pd.Panel({"on ...