USACO 2009 Open Treasure Cave /// DFS||并查集 oj26215
题目大意:
输入 p,n,t ;p为地点数 判断 t 能否回到源点1
接下来n行 每行输入 a b c; a能到达b和c
13 6 7
6 7 8
2 3 4
10 11 12
8 9 10
1 2 13
4 5 6
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的更多相关文章
- 分珠(dfs+并查集)
1140 分珠 时间限制:500MS 内存限制:65536K提交次数:24 通过次数:18 题型: 编程题 语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不 ...
- Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)
<题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...
- CodeForces - 455C Civilization (dfs+并查集)
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- CF 115 A 【求树最大深度/DFS/并查集】
CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outp ...
- 51nod1307(暴力树剖/二分&dfs/并查集)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...
- ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集
Untrusted Patrol Time Limit: 3 Seconds Memory Limit: 65536 KB ...
随机推荐
- DataWorks2.0的“业务流程”与1.0的“工作流”的对比
DatwWorks终于升级2.0了,心情万分激动之余,又有一丝担忧.因为,没法再创建新的旧版工作流了...新版抛弃了“工作流”这个概念,引入了“业务流程”和“解决方案”两个新的概念.于是,作为团队Le ...
- storm集群的安装
storm图解 storm的基本概念 Topologies:拓扑,也俗称一个任务 Spoults:拓扑的消息源 Bolts:拓扑的处理逻辑单元 tuple:消息元组,在Spoults和Bolts传递数 ...
- 移动端图片轮播效果:depth模式总结
最近公司app改版首页增加了一处轮播图效果,但是跟普通的轮播效果不同,是类似于下图的样式,找了一些兼容移动端的插件以及jQuery源码,总结一下使用心得: 1:jquery源码:缺点是在手机端的滑动很 ...
- post请求传文件
public static JSONObject doFormDataPost(File file, String sURL) throws IOException { HttpClient cont ...
- Python字典列表字段重组形成新的字典
最近遇到这样一个需求,需要将字典列表中的字段进行重组,形成一个新的字典.举个例子吧: l1 = [{"x": 22, "y": 22, "demand ...
- csp-s模拟测试95
csp-s模拟测试95 去世场祭. $T1$:这不裸的除法分块吗. $T2$:这不裸的数据结构优化$Dp$吗. $T3$:这不裸的我什么都不会搜索骗$30$分吗. 几分钟后. 这除法分块太劲了..(你 ...
- [JZOJ 5811] 简单的填数
题意:自己搜吧... 思路: 记二元组\((x,l)\)表示当前为\(x\)且之前有\(l\)个连续数与\(x\)相同. 并且维护up和low数组表示取到最大/最小值时,连续序列的长度. 正一遍,反一 ...
- LeetCode 704. Binary Search (二分查找)
题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime: 0 ms, faster than 100 % Memory Usage ...
- SpringBoot-集成PageHelper5.1.2踩坑
背景就不介绍了,项目是SpringBoot+MyBatis搭建的,需要集成git上的PageHelper5.1.2,这个插件大家都比较熟悉了 之前一直用的PageHelper4.0.3,集成是这样的: ...
- POJ 2763 /// 基于边权的树链剖分
题目大意: 给定n个结点,有n-1条无向边,给定每条边的边权 两种操作,第一种:求任意两点之间路径的权值和,第二种:修改树上一点的权值. 因为是一棵树,可以直接把 u点和v点间(假设u为父节点,v为子 ...