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. UVA1660 Cable TV Network (无向图的点连通度)

    题意:求一个无向图的点连通度. 把一个点拆成一个入点和一个出点,之间连一条容量为1的有向边,表示能被用一次.最大流求最小割即可. 一些细节的东西:1.源点固定,汇点要枚举一遍,因为最小割割断以后会形成 ...

  2. Android(java)学习笔记140:常用的对话框

    一.常见对话框属性: 1. AlertDialog.Builder属性  • setTitle: 为对话框设置标题 :• setIcon : 为对话框设置图标:• setMessage: 为对话框设置 ...

  3. 在web应用中使用日志

    Log4J是Jakarta下的一个开源代码的子项目,用Log4J,我们可以使用定制的格式,把调试信息和日志信息输出到一个或多个需要的地方. 在Web应用中一般使用一个专门的Servlet来完成Log4 ...

  4. feature map计算大小公式

    http://blog.csdn.net/cheese_pop/article/details/51955915 将整个分成两部分,左边部分,右边部分.右边部分每次其实都是移动stride这么大,左边 ...

  5. python_110_反射

    class Dog(object): def __init__(self,name): self.name=name def eat(self): print('%s is eating '%self ...

  6. python基本排序算法(一)

    一.冒泡排序 这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端(升序或降序排列),就如同碳酸饮料中二氧化碳的气泡最终会上浮到顶端一样,故名“冒泡排序”. 冒泡排序算法的原理如下: 比 ...

  7. iterator方法和for方法 遍历数据库user表结果集ResultSet

    首先,把连接数据库的语句做成工具类,因为会一直用到这几句 代码如下: package com.swift.jdbc; import java.sql.Connection; import java.s ...

  8. linux中添加一个用户到指定用户组的两种方式,修改一个用户到指定用户组的一种方式

    添加一个用户到指定用户组: gpasswd –a 用户名 组名usermod –G 组名 用户名 //第一种:gpasswd –a 用户名 组名 [root@localhost ~]# id user ...

  9. ECshop二次开发 ECSHOP首页显示积分商城里的商品

    以ECSHOP2.7.2官方默认模板为基础 1).首先打开 index.php 文件,在最末尾增加下面函数,注意千万不要写到 “?>” 的外面去,要加在“?>”的前面,加以下代码: /** ...

  10. springboot 修炼之路

    网上无意中发现一份关于springboot的教程说明,说的很详细,大家可以参考.具体地址:http://www.spring4all.com/article/246