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)
设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...
随机推荐
- 【BZOJ-2768】冠军调查 最小割
2768: [JLOI2010]冠军调查 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 971 Solved: 661[Submit][Status ...
- HA模式强制手动切换:IPC's epoch [X] is less than the last promised epoch [X+1]
-- ::, WARN org.apache.hadoop.hdfs.qjournal.client.QuorumJournalManager: Remote journal failed to wr ...
- 【bzoj2809】 Apio2012—dispatching
http://www.lydsy.com/JudgeOnline/problem.php?id=2809 (题目链接) 题意 给出一棵树,每个节点有两个权值${c}$,${L}$,分别代表花费和领导力 ...
- Windows XP/Windows 7/Windows 8/Windows 10系统封装的另类教程和思路
如果是早些年,XP时代的Ghost封装,各种的封装工具和驱动只能安装工具满天飞,比如龙帝国,还有很早用C++写的忘了什么名字了,自由天空的,非常的多: 当时为什么要用Ghost和用这些驱动安装工具以及 ...
- hdu 4609 FFT
题意:给出一堆数,问从这些数中取3个能组成三角形的概率? sol:其实就是问从这些数里取3个组成三角形有多少种取法 脑洞大开的解法:用FFT 设一开始的数是1 3 3 4 作一个向量x,其中x[i]= ...
- GPU keylogger && GPU Based rootkit(Jellyfish rootkit)
catalog . OpenCL . Linux DMA(Direct Memory Access) . GPU rootkit PoC by Team Jellyfish . GPU keylogg ...
- log4net配置和获取ILog实例
名称 描述 File 文件路径,如果RollingStyle为Composite或Date,则这里设置为目录,文件名在DatePattern里设置,其他则这里要有文件名.已经扩展支持虚拟目录 Roll ...
- CSS3系列四(Media Queries移动设备样式)
viewport设置适应移动设备屏幕大小 viewport:允许开发者创建一个虚拟窗口并自定义其窗口的大小或缩放功能 <meta name="viewport" conten ...
- UML的目标
http://zhidao.baidu.com/link?url=ghQvzG70vSCSLyQcrHDTd7xt1aSWBR73lPIMxBCEPo1ktkq9cQ3EE9TXX1mZyHINkVA ...
- c++编译错误提示及解决
IntelliSense: #error 指令: Please use the /MD switch for _AFXDLL builds 修改设置:工程(Project)-> 属性(Prope ...