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.

Input

There 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 ( <= N <= ), M ( <= M <= ) and K ( <= K <= N).

The next line contains K distinct integers indicating the indexes of piles (-based) that have sensors installed. The following M lines, each line contains two integers Ai and Bi ( <= Ai, Bi <= N) which indicates a bidirectional passageway connects piles Ai and Bi.

Then, there is an integer L ( <= 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.

Output

For each test case, output "Yes" if the security man worked normally and has checked all piles of drinks, or "No" if not.

Sample Input


Sample Output

No
Yes

Author: DAI, Longao
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

据说这题是bfs裸题,事实也是如此。

这里用两种实现方法,dfs和bfs,实际上这两种方法差不多。

注意两种特判条件。

dfs

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<map>
using namespace std;
#define N 100006
int n,m,k;
vector<int> v[N];
set<int> s;
int tag[N];
int vis[N];
int a[N];
void dfs(int st){
vis[st]=;
for(int i=;i<v[st].size();i++){
int u=v[st][i];
if(!vis[u]){
if(tag[u]) s.insert(u);
else dfs(u);
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){ for(int i=;i<N;i++){
v[i].clear();
}
memset(tag,,sizeof(tag));
s.clear();
memset(vis,,sizeof(vis)); scanf("%d%d%d",&n,&m,&k);
int x,y;
for(int i=;i<k;i++)scanf("%d",&x);
for(int i=;i<m;i++){
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
int L;
scanf("%d",&L);
for(int i=;i<L;i++){
scanf("%d",&a[i]);
tag[a[i]]=;
}
if(L<k){
printf("No\n");
continue;
}
dfs(a[]); int flag=;
for(int i=;i<L;i++){
if(s.find(a[i])==s.end()){
flag=;
break;
}
else{
s.erase(a[i]);
dfs(a[i]);
}
} for(int i=;i<=n;i++){
if(!vis[i]){
flag=;
break;
}
}
if(flag==){
printf("Yes\n");
}else{
printf("No\n");
} }
return ;
}

bfs

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define N 100006
int n,m,k;
vector<int> v[N];
set<int> s;
int tag[N];
int vis[N];
int a[N];
void bfs(int st){
queue<int>q;
q.push(st);
vis[st]=;
while(!q.empty()){
int tmp=q.front();
q.pop();
for(int i=;i<v[tmp].size();i++){
int u=v[tmp][i];
if(!vis[u]){
if(tag[u]) s.insert(u);
else q.push(u);
vis[u]=;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){ for(int i=;i<N;i++){
v[i].clear();
}
memset(tag,,sizeof(tag));
s.clear();
memset(vis,,sizeof(vis)); scanf("%d%d%d",&n,&m,&k);
int x,y;
for(int i=;i<k;i++)scanf("%d",&x);
for(int i=;i<m;i++){
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
int L;
scanf("%d",&L);
for(int i=;i<L;i++){
scanf("%d",&a[i]);
tag[a[i]]=;
}
if(L<k){
printf("No\n");
continue;
}
bfs(a[]); int flag=;
for(int i=;i<L;i++){
if(s.find(a[i])==s.end()){
flag=;
break;
}
else{
s.erase(a[i]);
bfs(a[i]);
}
}
for(int i=;i<=n;i++){
if(!vis[i]){
flag=;
break;
}
}
if(flag) printf("Yes\n");
else printf("No\n");
}
return ;
}

zoj 3811 Untrusted Patrol(bfs或dfs)的更多相关文章

  1. ZOJ 3811 Untrusted Patrol

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811 解题报告:一个无向图上有n个点和m条边,其中有k个点上安装 ...

  2. ZOJ 3811 Untrusted Patrol The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

    Description Edward is a rich man. He owns a large factory for health drink production. As a matter o ...

  3. ZOJ 3811 Untrusted Patrol【并查集】

    题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点 思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序WA了几发, ...

  4. ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

    Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB    ...

  5. zoj3811 Untrusted Patrol (dfs)

    2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onl ...

  6. HDU-4607 Park Visit bfs | DP | dfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...

  7. BFS和DFS详解

    BFS和DFS详解以及java实现 前言 图在算法世界中的重要地位是不言而喻的,曾经看到一篇Google的工程师写的一篇<Get that job at Google!>文章中说到面试官问 ...

  8. 算法录 之 BFS和DFS

    说一下BFS和DFS,这是个比较重要的概念,是很多很多算法的基础. 不过在说这个之前需要先说一下图和树,当然这里的图不是自拍的图片了,树也不是能结苹果的树了.这里要说的是图论和数学里面的概念. 以上概 ...

  9. hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. [Angular 2] Build a select dropdown with *ngFor in Angular 2

    We want the start-pipe more flexable to get param, so when using it, we pass a second param as statu ...

  2. Hadoop 开源调度系统zeus(二)

    紧跟之前Hadoop 开源调度系统zeus(一) 本节主要介绍一下zeus的架构: 先给一个zeus的架构图 无论Master还是Worker都有一套WEB UI,无论从哪个上面去看,看到的结果都是一 ...

  3. tail-head

    [root@rusky]# tail test3 #不加参数默认显示全部内容 line line2 line3 line4 line5 line6 line7 line8 line9 line10 [ ...

  4. 用MVC4练习,后台用aspx,数据库DemoDb《MvcUserDemo》

    将ado.net的cs文件SqlHelper.cs放入解决方案 using System; using System.Collections.Generic; using System.Linq; u ...

  5. .net I/O操作 导图

    稍微总结下,System.IO提供了四种类型来实现,对单个文件和计算机目录结构的操作.Directory和File通过静态成员实现建立.删除.复制和移动操作(上图没有提及).而FileInfo和Dir ...

  6. ORA-01172 ORA-01151

    今天发现服务器的9号硬盘坏了,因做了RAID5,报障后也没理会,过了一会儿,有人反应说报表运行不出来,发现服务器所有的硬盘都不工作了,界面也没有反应,就强行关机,再开机可以进入系统,等维修人员来换了新 ...

  7. 读书笔记_Effective_C++_条款二十四: 若所有参数皆需类型转换,请为此采用non-member函数

    class A { private: int a; public: A(int x) :a(x){} A operator*(const A& x) { return A(a*x.a); } ...

  8. Android 命令

    连接调试 adb connect 127.0.0.1:6555

  9. noip2015运输计划

    二分+LCA+查分前缀和 #include<iostream> #include<cstring> #include<cstdio> #include<alg ...

  10. 会话控制session,cookie(0521)

    简单介绍: 一.什么是session? 1. 定义: Session,在计算机中,尤其是在网络应用中,称为“会话”.在计算机专业术语中,Session是指一个终端用户与交互系统进行通信的时间间隔,通常 ...