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 (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 转自:http://blog.csdn.net/napoleon_acm/article/details/39124615

题目: LINK

给定一个无向图,n个点,  m条边,k个特殊点(有传感器),只有当第一次到达特殊点的时候才会发出信号,给出发出信号的序列,问是否存在这样的路径使得每个点至少遍历一次,而且特殊点第一次到达的顺序和和题目输入一样。  (1 <= N <= 100000), M (1 <= M <= 200000)  先特判 如果询问时输入的L<k, 那么直接No, 因为l<k肯定有传感器的点没有到达,不满足每个点都遍历一次。 先把第一个特殊点入队,遍历所有的可以到达的点(中途不经过其他特殊点),标记为可以到达。  之后判断第二个点是否可以从第一个点到达(是否被标记),如果不可以则No,否则遍历从第二个特殊点出发的可以到达的点(同样中途不经过剩余特殊点). ....... 依次处理完所有点即可. 如果前面的一个特殊点可以到达某个点,那么 他后面的特殊点一定也可以到达这些点,因为他可以回到前面的特殊点再走过去.

本题可用多种思路做,但是拍代码前一定要先分析好再开拍,不然,,,

可用方法:bfs、dfs、并查集。

吐血ac。。。

代码:

bfs:

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,m,k,l;
int vis[N];
int vis2[N];
vector<int>bian[N];
int re[N];
int a,b;
vector<int>::iterator it; void ini()
{
int i;
memset(vis,,sizeof(vis));
memset(vis2,,sizeof(vis2));
for(i=;i<=;i++){
bian[i].clear();
}
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=k;i++){
scanf("%d",&re[i]);
}
while(m--){
scanf("%d%d",&a,&b);
bian[a].push_back(b);
bian[b].push_back(a);
}
scanf("%d",&l);
for(i=;i<=l;i++){
scanf("%d",&re[i]);
vis2[ re[i] ]=;
}
} int solve()
{
if(l!=k){
return ;
}
queue<int>q;
int r;
vis[ re[] ]=;
for(int i=;i<=l;i++){
// printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]);
if(vis[ re[i] ]==) return ;
// vis2[ re[i] ]=0;
q.push(re[i]);
while( q.size()> )
{
r=q.front();
//printf(" r=%d\n",r);
q.pop();
// vis[r]=1; for(it=bian[r].begin();it!=bian[r].end();it++){
// printf(" it=%d\n",*it);
if(vis[*it]==) continue;
vis[ *it ]=;
if( vis2[ *it ]== ){
q.push( (*it) );
}
}
/*
for(int j=0;j<(int)bian[r].size();j++){
int y=bian[r][j];
if(vis[y]==1) continue;
vis[y]=1;
if(vis2[y]==0){
q.push(y); }
}*/
}
} for(int i=;i<=n;i++){
if(vis[i]==) return ;
}
return ;
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
if(solve()==){
printf("No\n");
}
else{
printf("Yes\n");
}
} return ;
}

dfs版本:

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,m,k,l;
int vis[N];
int vis2[N];
vector<int>bian[N];
int re[N];
int a,b; void ini()
{
int i;
memset(vis,,sizeof(vis));
memset(vis2,,sizeof(vis2));
for(i=;i<=;i++){
bian[i].clear();
}
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=k;i++){
scanf("%d",&re[i]);
}
while(m--){
scanf("%d%d",&a,&b);
bian[a].push_back(b);
bian[b].push_back(a);
}
scanf("%d",&l);
for(i=;i<=l;i++){
scanf("%d",&re[i]);
vis2[ re[i] ]=;
}
} void dfs(int s)
{
vector<int>::iterator it;
for(it=bian[s].begin();it!=bian[s].end();it++){
if(vis[*it]==) continue;
vis[*it]=;
if(vis2[*it]==){
dfs(*it);
}
}
} int solve()
{
if(l!=k){
return ;
}
vis[ re[] ]=;
for(int i=;i<=l;i++){
// printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]);
if(vis[ re[i] ]==) return ;
dfs(re[i]);
} for(int i=;i<=n;i++){
if(vis[i]==) return ;
}
return ;
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
if(solve()==){
printf("No\n");
}
else{
printf("Yes\n");
}
} return ;
}

并查集版:http://blog.csdn.net/qq574857122/article/details/39121391

转自:

思路:

先把无触发器的点放到图里。

然后根据触发器的信号顺序把点依次加入图中,加入时只添加(与无触发器点相连的边)

然后判断这个点能否与之前的图连通。bfs+并查集判断。

==其实并查集就够了,写的时候有脑洞,bfs就冒出来了

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,m,k,l;
int f[N];
int rank[N];
int vis2[N];
vector<int>bian[N];
int re[N];
vector<int>::iterator it; int find(int v)
{
//printf(" v=%d f=%d\n",v,f[v]);
return f[v]=f[v]==v ? v : find(f[v]);
} void merge(int x,int y)
{
int a=find(x),b=find(y);
if(a==b) return;
if(rank[a]<rank[b]){
f[a]=b;
}
else{
f[b]=a;
if(rank[b]==rank[a]){
++rank[a];
}
}
} void ini()
{
int i;
int a,b;
//memset(vis,0,sizeof(vis));
memset(rank,,sizeof(rank));
memset(vis2,,sizeof(vis2));
for(i=;i<=;i++){
bian[i].clear();
} scanf("%d%d%d",&n,&m,&k);
for(i=;i<=n;i++){
f[i]=i;
// printf(" i=%d f=%d\n",i,f[i]);
}
for(i=;i<=k;i++){
scanf("%d",&re[i]);
}
while(m--){
scanf("%d%d",&a,&b);
bian[a].push_back(b);
bian[b].push_back(a);
}
scanf("%d",&l);
for(i=;i<=l;i++){
scanf("%d",&re[i]);
vis2[ re[i] ]=;
}
} int solve()
{
int i,x,y;
if(l!=k){
return ;
} // for(i=1;i<=n;i++){
// printf(" i=%d f=%d\n",i,f[i]);
// }
// vis[ re[1] ]=1;
for(i=;i<=n;i++){
if(vis2[i]==) continue;
for(it=bian[i].begin();it!=bian[i].end();it++){
if(vis2[*it]==) continue;
merge(i,*it);
}
} i=;
for(it=bian[ re[i] ].begin();it!=bian[ re[i] ].end();it++){
if(vis2[*it]==) continue;
merge(re[i],*it);
}
vis2[ re[] ]=; for(i=;i<=l;i++){
// printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]);
for(it=bian[ re[i] ].begin();it!=bian[ re[i] ].end();it++){
if(vis2[*it]==) continue;
merge(re[i],*it);
}
x=find(re[i-]);
y=find(re[i]);
//printf(" re[i-1]=%d re[i]=%d x=%d y=%d\n",re[i-1],re[i],x,y);
if(x!=y) return ;
vis2[ re[i] ]=;
}
x=find();
for(i=;i<=n;i++){
if(x!=find(i)) return ;
}
return ;
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
if(solve()==){
printf("No\n");
}
else{
printf("Yes\n");
}
} return ;
}

ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集的更多相关文章

  1. hdu5441(2015长春赛区网络赛1005)类最小生成树、并查集

    题意:有一张无向图,一些点之间有有权边,某条路径的值等于路径上所有边的边权的最大值,而某个点对的值为这两点间所有路径的值的最小值,给出多个询问,每个询问有一个值,询问有多少点对满足其值小于等于询问值. ...

  2. ZOJ 3810 A Volcanic Island (2014年牡丹江赛区网络赛B题)

    1.题目描写叙述:点击打开链接 2.解题思路:本题是四色定理的模板题.只是有几种情况要提前特判一下:n==1直接输出,1<n<5时候无解,n==6时候套用模板会出现同样的块.因此要特判一下 ...

  3. ZOJ 3814 Sawtooth Puzzle (2014年牡丹江赛区网络赛F题)

    1.题目描写叙述:点击打开链接 2.解题思路:本题是一道隐式图的搜索题目.一般来说,这类题目首先要定义状态,接下来是弄清楚状态怎样转移,以及状态怎样判重,怎样推断当前状态是否和目标状态同样.至于求解最 ...

  4. hdu5438(2015长春赛区网络赛1002)拓扑序+DFS

    题意:给出一张无向图,每个节点有各自的权值,问在点数为奇数的圈中的点的权值总和是多少. 通过拓扑序的做法标记出所有非圈上的点,做法就是加每条边的时候将两点的入度都加一,然后将所有度数为1的点入队,删去 ...

  5. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  6. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  7. ICPC 2018 徐州赛区网络赛

    ACM-ICPC 2018 徐州赛区网络赛  去年博客记录过这场比赛经历:该死的水题  一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进.     D. Easy Math 题意:   ...

  8. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】

    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛  M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...

  9. HDU 5875 Function -2016 ICPC 大连赛区网络赛

    题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...

随机推荐

  1. WPF知识点全攻略05- XAML内容控件

    此处简单列举出布局控件外,其他常用的控件: Window:WPF窗口 UserControl:用户控件 Page:页 Frame:用来浏览Page页 Border:嵌套控件,提供边框和背景. Butt ...

  2. 解决Genymotion2.8.1在拖动安装APK文件出现ARMtranslate错误

    转载文章:http://blog.csdn.net/solo_talk/article/details/68488129 在新版本的genymotion中,我们拖动安装APK文件的时候会出现一个问题, ...

  3. tomcat中如何禁止和允许主机或地址访问

    1.tomcat中如何禁止和允许列目录下的文件 在{tomcat_home}/conf/web.xml中,把listings参数设置成false即可,如下: <servlet>...< ...

  4. C++笔记(仅C++特性,需C语言基础)

    C++复习笔记一(类的声明定义应用与构造函数析构函数部分)const在C语言中是"不能被改变值的变量",而在C++种子则是"一种有类型描述的常量",常量必须初始 ...

  5. Android Studio问题记录

    1>Android Studio中module是什么,? 答:Android Studio是基于intellij,跟eclipse不太一样.对应关系如下: intellij的project -- ...

  6. MySQL 资料库概论与MySQL 安装

    本文来自:https://www.breakyizhan.com/sql/5648.html 1. 储存与管理资料 储存与管理资料一直是资讯应用上最基本.也是最常见的技术.在还没有使用电脑来管理你的资 ...

  7. docker系列之基础命令-1

    1.docker基础命令 docker images 显示镜像列表 docker ps 显示容器列表 docker run IMAGE_ID 指定镜像, 运行一个容器 docker start/sto ...

  8. 如何创作用纯 CSS 绘制一支栩栩如生的铅笔

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/PaZYBw 可交互视频教 ...

  9. Vue—事件修饰符

    Vue事件修饰符 Vue.js 为 v-on 提供了事件修饰符来处理 DOM 事件细节,如:event.preventDefault() 或 event.stopPropagation(). Vue. ...

  10. mbist summary

    1. 关于mbist,网上也有介绍,觉得不错: 推荐的mbistt的博客:奋斗的猪 2.使用的工具是mbistarchitect,不是tessent. 3.工具使用的相关文档:从EETOP和工具自带的 ...