首页 技术 正文
技术 2022年11月15日
0 收藏 643 点赞 3,326 浏览 3806 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839