zoj 3811 Untrusted Patrol(bfs或dfs)
2015-08-29 18:37
471 查看
Untrusted PatrolTime Limit: 3 Seconds Memory Limit: 65536 KB
Author: DAI, Longao
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round
据说这题是bfs裸题,事实也是如此。
这里用两种实现方法,dfs和bfs,实际上这两种方法差不多。
注意两种特判条件。
dfs
View Code
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 (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.
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
2 5 5 3 1 2 4 1 2 2 3 3 1 1 4 4 5 3 4 2 1 5 5 3 1 2 4 1 2 2 3 3 1 1 4 4 5 3 4 1 2
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> #include<queue> using namespace std; #define N 100006 int n,m,k; vector<int> v ; set<int> s; int tag ; int vis ; int a ; void bfs(int st){ queue<int>q; q.push(st); vis[st]=1; while(!q.empty()){ int tmp=q.front(); q.pop(); for(int i=0;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]=1; } } } } int main() { int t; scanf("%d",&t); while(t--){ for(int i=0;i<N;i++){ v[i].clear(); } memset(tag,0,sizeof(tag)); s.clear(); memset(vis,0,sizeof(vis)); scanf("%d%d%d",&n,&m,&k); int x,y; for(int i=0;i<k;i++)scanf("%d",&x); for(int i=0;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=0;i<L;i++){ scanf("%d",&a[i]); tag[a[i]]=1; } if(L<k){ printf("No\n"); continue; } bfs(a[0]); int flag=1; for(int i=1;i<L;i++){ if(s.find(a[i])==s.end()){ flag=0; break; } else{ s.erase(a[i]); bfs(a[i]); } } for(int i=1;i<=n;i++){ if(!vis[i]){ flag=0; break; } } if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }
View Code
相关文章推荐
- HTML:5的新特性之音频和视频播放
- Spring MVC全局异常后返回JSON异常数据
- 二进制文件的读入
- linux中注册共享中断
- <PY>计算信息熵
- MR案例:小文件合并SequeceFile
- POJ题目3229 Facer’s string(后缀数组求a串长度为k子串有几个出现在b串)
- 硬黑客:智能硬件生死之战
- Java 反射 Reflection
- 360 QConf配置管理系统简易部署和使用 | 峰云就她了
- Squid服务基础及构建代理服务器
- C#连接数据库增删改查代码 SQL SERVER/ACCESS 通用类
- 利用python收发邮件功能实现远程电脑的控制
- 初始化那点小事
- LaTeX中加入Matlab中生成的图片
- 初始化那点小事
- 【ssh】struts2中action接收参数的方法
- 第4讲:HOOK 任务管理器 无法结束进程
- JavaScript中的this关键字
- Ubuntu 14.04中修复“update information is outdated”错误