zoj3811 Untrusted Patrol (dfs)
2014牡丹江网络赛C题 (第三水的题
The 2014 ACM-ICPC Asia Mudanjiang Regional First Round
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811
|
Untrusted Patrol
Time Limit: 3 Seconds Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hired a security man to patrol the warehouse. The warehouse has N piles of drinks and M passageways connected them (warehouse is not big enough). When the evening comes, the security man will start to patrol the warehouse following a path to check all piles of drinks. Unfortunately, Edward is a suspicious man, so he sets sensors on K piles of the drinks. When the security man comes to check the drinks, the sensor will record a message. Because of the memory limit, the sensors can only record for the first time of the security man's visit. After a peaceful evening, Edward gathered all messages ordered by recording time. He wants to know whether is possible that the security man has checked all piles of drinks. Can you help him? The security man may start to patrol at any piles of drinks. It is guaranteed that the sensors work properly. However, Edward thinks the security man may not works as expected. For example, he may digs through walls, climb over piles, use some black magic to teleport to anywhere and so on. InputThere are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case: The first line contains three integers N (1 <= N <= 100000), M (1 <= M <= 200000) and K (1 <= K <= N). The next line contains K distinct integers indicating the indexes of piles (1-based) that have sensors installed. The following M lines, each line contains two integers Ai and Bi (1 <= Ai, Bi <= N) which indicates a bidirectional passageway connects piles Ai and Bi. Then, there is an integer L (1 <= L <= K) indicating the number of messages gathered from all sensors. The next line contains L distinct integers. These are the indexes of piles where the messages came from (each is among the K integers above), ordered by recording time. OutputFor each test case, output "Yes" if the security man worked normally and has checked all piles of drinks, or "No" if not. Sample Input2 Sample OutputNo Author: DAI, Longao Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round |
题意:
给出一个无向图,一些点上有监视器,监视器能在维修工第一次到达该点时记录,给出所有监视器记录的顺序,判断维修工是否有可能在图中走完所有点。
输入:T组数据,每组第一行n,m,k,表示有n个点m条双向边,k个点有监视器。下一行有k个点表示装监视器的点的编号。然后m行是m条边。然后一个L是有记录的监视器的数量,下面一行是L个点,表明这些监视器按这个顺序记录了。
题解:DFS。
先判L不等于K的话,它就有点没走到,直接判no。
然后开个数组b[],b[x]记录x点是由第几个监视器走到的。初始全部标为0,各个有监视器的点按最后一行输入的顺序标上1,2,3...L。
考虑监视器记录的情况,维修工需要在不经过3~L的情况下从1走到2,然后在不经过4~L的情况下从2走到3,依此类推。因为只记录第一次,走过的点是可以随便走的,所以从2走到3也就是从1或者2能到达的点走到3。这样,我们就每次从一个点dfs找看在不经过更高编号的点的情况下有没有路走到之前低编号的点走到的点。
从1~L号监视器的点依次开始dfs,不能经过b[x]比起始点高的点,只走b[x]=0的点,把走过的点标为b[x]=起点的b[]。并记录这次dfs是否能走到b[]比起始点小的点,不能的话就输出no。一直1~L都不no的话就输出yes。
具体是这样:我们有一个超碉变量now,表明这次dfs的起点。我们先从监视器1开始dfs,now=1,不能经过b[x]>1的点,并且把走过顶点都标为b[x]=1。
然后再从2开始,now=2,如果有边连向b[]<2的点,说明能走到比它小的监视器,把flag标为1。同样,这个dfs不能经过b[x]>2的点。
就这样对1~L号监视器都进行dfs。途中如果有一次dfs完flag为0,说明这个监视器和之前的监视器之间没有能走的路,就no。
代码不长,应该能直接看懂
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
const double eps=1e-;
const double pi=acos(-1.0); const int maxn=;
const int maxm=; struct Edge {
int v,next;
} e[maxm];
int head[maxn],en; void add(int x,int y) {
e[en].v=y;
e[en].next=head[x];
head[x]=en++;
} int n,m,k,l;
int a[maxn]; int b[maxn];
int now;
bool flag; void dfs(int x) {
//printf("[%d %d]\n",now,x);
b[x]=now;
for(int i=head[x]; i!=-; i=e[i].next) {
int v=e[i].v;
if(b[v]) {
if (b[v]<now) {
//printf("->%d b[%d]=%d\n",v,v,b[v]);
flag=;
}
continue;
}
dfs(v);
}
return;
} bool farm() {
if(l<k)return false;
int i;
mz(b);
FOR(i,,l) b[a[i]]=i;
now=;
dfs(a[]);
FOR(i,,l) {
flag=;
now=i;
dfs(a[i]);
if(!flag)return false;
}
FOR(i,,n)if(!b[i])return false;
return true;
} int main() {
int T,x,y,i;
scanf("%d",&T);
while(T--) {
scanf("%d%d%d",&n,&m,&k);
REP(i,k) {
scanf("%d",&x);
}
en=;
memset(head,-,sizeof(head));
REP(i,m) {
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
scanf("%d",&l);
FOR(i,,l)scanf("%d",&a[i]);
if(farm()) puts("Yes");
else puts("No");
}
return ;
}
zoj3811 Untrusted Patrol (dfs)的更多相关文章
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 搜索——深度优先搜索(DFS)
设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...
随机推荐
- Leetcode 270. Closest Binary Search Tree Value
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- 【BZOJ-1941】Hide and Seek KD-Tree
1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 830 Solved: 455[Submi ...
- 【BZOJ-2893】征服王 最大费用最大流(带下界最小流)
2893: 征服王 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 156 Solved: 48[Submit][Status][Discuss] D ...
- Jenkins离线下载插件,并安装
在jenkins点击插件进入wiki,然后点击列表下载一个版本的包 安装是,选择高级的tab,下面就有安装: 还有一种方法,就是装VPN,在高级设置VPN地址.
- STL之lower_bound和upper_bound
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...
- Appium运行时,error: Logcat capture failed: spawn ENOENT的解决办法
目前发现有以下两种可能: 一:查看环境变量是否配置成功. ANDROIDSDK D:\my_2_softwares\JAVA\adt-bundle-windows-x86-20140702\sdkPA ...
- 旅图beta版 asp.net web api 单元测试
旅图 beta版 asp.net web api 单元测试 测试接口:http://120.27.7.115:1010/Help 测试目的 对每个接口单元进行测试,保证每个接口的可靠性. 单元描述 注 ...
- Beta Daily Scrum 第三天
[目录] 1.任务进度 2.困难及解决 3.燃尽图 4.代码check-in 5.总结 1. 任务进度 学号 今日完成 明日完成 612 初步完成成就界面的统计图表 继续编写成就界面的图表 615 白 ...
- R in bioinformatic
TCGA https://www.bioconductor.org/packages/release/bioc/vignettes/TCGAbiolinks/inst/doc/tcgaBiolinks ...
- JS-计算器制作
不完善,接下来想着把运算符分开成一个一个的按钮... <!DOCTYPE html><html> <head> <meta charset="UTF ...