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 ...
随机推荐
- delphi 获取所有窗口标题
unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...
- NX二次开发-将对象移动到图层UF_OBJ_set_layer
#include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <uf_layer.h&g ...
- selenium+python 绕过登录进行测试
多个Py文件进行多线程测试时,每次登录是很麻烦的事情,所以通过cookie登录,可以绕过登录操作 但是至少需要正常登录一次才能获取到cookie 然后使用cookies进行登录
- uoj118 【UR #8】赴京赶考
题目 不难发现我们直接走过去就行了 考虑到第\(i\)行的构造方法就是把\(b\)数组作为模板,每个数和\(a_i\)异或一下就可以了 于是不难发现对于一段连续相等的\(a\),它们在矩阵上就形成了完 ...
- 牛客B-Xor Path /// 求所有Path( i->j )( j >= i )路径的异或和
题目大意: https://ac.nowcoder.com/acm/contest/272/B?&headNav=acm 给定一棵n个点的树,每个点有权值.定义表示 到 的最短路径上,所有 ...
- GNU 交叉工具链的介绍与使用
常用工具介绍 名称 归属 作用 armlinuxas binutils 编译 ARM 汇编程序 armlinuxar binutils 把多个.o 合并成一个.o 或静态库(.a) arml ...
- VBA提取HTML文件信息
Sub test() Dim html As Object, D As Object, W As Object, arr() Set html = CreateObject("m ...
- GetOpenFilename的基本用法
GetOpenFilename '一.概述基本语法 Application.GetOpenFilename 方法 显示标准的“打开”对话框,并获取用户文件名,而不必真正打开任何文件,只是把打开文件名称 ...
- 【模板篇】k短路 SDOI2010 魔法猪学院
题目传送门 吐槽时间 题目分析 代码 题目の传送门 都成了一道模板题了OvO ============================================================= ...
- mysql-connetor-c 自动创建数据库、数据库表的命令
1.首先连接MySQL默认的数据库mysql // 参数说明: // strIP: MySQL数据库的IP地址 // nPort: MySQL数据库的端口号 // strDBName: 要连接的数据库 ...