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. idea报错:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configu

    java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more tha ...

  2. c#写出乘法口诀

    显然是显得无聊五分钟写的乘法口诀 static void Main(string[] args)        {            int dq;            int[] array ...

  3. Ubuntu 18.04 上使用 OpenJDK 安装并运行 Tomcat

    在Linux上安装与卸载JDK和JRE,两种常用方法: 一.通过 apt-get 命令在线进行安装与卸载(会自动配置好环境变量) 二.通过下载并解压 .tar.gz 包进行手动安装与手动卸载(需要手动 ...

  4. No-11.变量进阶

    变量进阶 目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是保存在 内存 中的 在 Python 中 函数 的 参数传递 以及 返回值 都是靠 引用 传递 ...

  5. Mac 下 Android Studio 安装

    给大家介绍下 Mac Os 系统下的 Android Studio 的安装吧,二者步骤类似. 方法/步骤   1 首先下载 Mac 环境下的 Android Studio 的安装包,为 dmg 格式的 ...

  6. NoSQL 之 Morphia 操作 MongoDB

    上两篇文章:http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html http://www.cnblogs.com/hoojo/arch ...

  7. Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束

    (aspect oriented programming面向切面编程) 首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression ...

  8. 【Java_多线程并发编程】基础篇——线程状态扭转函数

    1. wait() sleep() yield() join()用法与区别 本文提到的当前线程是指:当前时刻,获得CPU资源正在执行的线程. 1.1 wait()方法 wait()方法定义在Objec ...

  9. DRF框架中的演变View

    import json from django.db import DatabaseError from django.http import HttpResponse from django.htt ...

  10. joyoi1957 「Poetize5」Vani和Cl2捉迷藏

    最小路径可重点覆盖.先传递闭包,然后拆点,\(n-\)最大匹配,看算法竞赛进阶指南. #include <iostream> #include <cstring> #inclu ...