zoj 3811 Untrusted Patrol(bfs或dfs)
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)的更多相关文章
- ZOJ 3811 Untrusted Patrol
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811 解题报告:一个无向图上有n个点和m条边,其中有k个点上安装 ...
- 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 ...
- ZOJ 3811 Untrusted Patrol【并查集】
题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点 思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序WA了几发, ...
- ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集
Untrusted Patrol Time Limit: 3 Seconds Memory Limit: 65536 KB ...
- zoj3811 Untrusted Patrol (dfs)
2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onl ...
- HDU-4607 Park Visit bfs | DP | dfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...
- BFS和DFS详解
BFS和DFS详解以及java实现 前言 图在算法世界中的重要地位是不言而喻的,曾经看到一篇Google的工程师写的一篇<Get that job at Google!>文章中说到面试官问 ...
- 算法录 之 BFS和DFS
说一下BFS和DFS,这是个比较重要的概念,是很多很多算法的基础. 不过在说这个之前需要先说一下图和树,当然这里的图不是自拍的图片了,树也不是能结苹果的树了.这里要说的是图论和数学里面的概念. 以上概 ...
- 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 ...
随机推荐
- 利用TreeSet给纯数字字符串排序
import java.util.Iterator;import java.util.TreeSet; /* * 给字符串中的数字排序 * String str = "10,2,11,1,3 ...
- Android设备中实现Orientation Sensor(图)兼谈陀螺仪
设备中的三自由度Orientation Sensor就是一个可以识别设备相对于地面,绕x.y.z轴转动角度的感应器(自己的理解,不够严谨).智能手机,平板电脑有了它,可以实现很多好玩的应用,比如说指南 ...
- iOS 画音频波形曲线 根据音频数据版
效果图 DrawView.h #import <UIKit/UIKit.h> @interface DrawView : UIView @property shortshort *draw ...
- POJ3692 Kindergarten 【最大独立集】
Kindergarten Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5317 Accepted: 2589 Desc ...
- javascript封装id|class|元素选择器
由于各个浏览器都支持的选择方法只有如下三种: 1 document.getElementById() 2 document.getElementsByName() 3 document.getElem ...
- linux-Python升级安装
Wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz tar zxvf Python-3.5.0.tar.gz && ...
- T-SQL 一次插入多行数据
使用 INSERT SELECT 向表中插入数据 --将t1中查询到的数据插入添加到t2中(t2表必须存在,且顺序.数据类型必须与t1一致) INSERT INTO t2(USERNAME,PASSW ...
- iPhone手机的屏幕尺寸、分辨率及适配
1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS ...
- osg添加纹理示例
转自http://www.cnblogs.com/ylwn817/articles/1976851.html #include <osgDB/ReadFile>#include <o ...
- 更好的使用chrome
Ctrl+tab 在标签页之间切换 Ctrl+1 到 Ctrl+8 切换到指定位置编号的标签页.您按下的数字代表标签页横条上的相应标签位置 Ctrl+9 ...