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 ;
}

最新文章

  1. 正则表达式 match 和 exec 比较
  2. Perl/Nagios – Can’t locate utils.pm in @INC
  3. google project tango 学习笔记
  4. ucenter 整合同步登录的内部实现原理及thinkphp整合ucenter
  5. maven pom.xml解释 (转)
  6. OpenCV中的常用函数
  7. cordova 插件开发
  8. 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类)
  9. java基础Haep(堆)和Stack(栈)区别
  10. vue px转换为rem
  11. (Dijkstra) POJ1797 Heavy Transportation
  12. HDU 2586 How far away(LCA+邻接表)
  13. Arduino——My-Clock项目 发布时间:2018-12-31
  14. 面向对象的Shell脚本
  15. 查看Ubuntu的版本和系统版本
  16. CreateDirectory 创建文件夹 C\C++
  17. 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)
  18. 2&lt;&lt;3=?
  19. CUDA、CUDNN在Mac Book Pro上安装的问题
  20. java回调方法之理解

热门文章

  1. (转)关于Android的nodpi,xhdpi,hdpi,mdpi,ldpi
  2. Java中的线程Thread总结
  3. Node.js 实现简单小说爬虫
  4. Android文字的复制和粘贴
  5. C语言之可重入函数 &amp;&amp; 不可重入函数
  6. 在Linux系统中如何把文件拷贝到U盘
  7. JAVA-FileInputStream之read方法
  8. WCF 客户端与服务端消息传输
  9. root用户安装的软件在普通用户不生效
  10. C/C++中虚函数的调用