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