题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点

思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序WA了几发,此时JYB用BFS秒了,索性最后还是调出来了,今天自己写了下,感觉唯一的坑点就是需要遍历完所有的点

//zoj3811

#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <queue>

#define maxn 500000

#define inf 0x3f3f3f3f

using namespace std;

int father[maxn],head[maxn],point[maxn],next[maxn];

int n,m,k,a,b[maxn],now,x[maxn],y[maxn];

bool mark[maxn];

int find(int x)

{

if(father[x]==x)return x;

return father[x]=find(father[x]);

}

void add(int x,int y)

{

next[++now]=head[x];

head[x]=now;

point[now]=y;

}

int main()

{

int t,xx,yy,l;

scanf("%d",&t);

while(t--)

{

int flag=0,z=0;

now=0;

memset(head,0,sizeof(head));

memset(mark,0,sizeof(mark));

scanf("%d%d%d",&n,&m,&k);

for(int i=1;i<=n;i++)father[i]=i;

for(int i=1;i<=k;i++)

{

scanf("%d",&a);

mark[a]=1;

}

for(int i=1;i<=m;i++)

{

scanf("%d%d",&xx,&yy);

add(xx,yy);add(yy,xx);

x[i]=xx;y[i]=yy;

}

scanf("%d",&l);

for(int i=1;i<=l;i++)scanf("%d",&b[i]);

mark[b[1]]=0;

//if(l!=k){printf("No\n");continue;}

for(int i=1;i<=m;i++)

{

if(mark[x[i]]||mark[y[i]])continue;

xx=find(x[i]);yy=find(y[i]);

if(xx!=yy)

{

father[xx]=yy;

z++;

//   printf("%d  %d",x[i],y[i]);

}

}

for(int i=2;i<=l;i++)

{

mark[b[i]]=0;

for(int j=head[b[i]];j;j=next[j])

{

int u=point[j];

if(mark[u]==1)continue;

xx=find(b[i]);yy=find(u);

if(xx!=yy){father[xx]=yy;z++;}

}

xx=find(b[i]);yy=find(b[i-1]);

if(xx!=yy)

{

flag=1;break;

}

}

for(int i=1;i<=n;i++)if(find(i)!=find(b[1]))flag=1;

if(flag==1)printf("No\n");

else printf("Yes\n");

}

return 0;

}

ZOJ 3811 Untrusted Patrol【并查集】的更多相关文章

  1. 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 ...

  2. zoj 3811 Untrusted Patrol(bfs或dfs)

    Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a l ...

  3. ZOJ 3811 Untrusted Patrol

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811 解题报告:一个无向图上有n个点和m条边,其中有k个点上安装 ...

  4. ZOJ:2833 Friendship(并查集+哈希)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2833 A friend is like a flower, a rose ...

  5. ZOJ 3321 Circle【并查集】

    解题思路:给定n个点,m条边,判断是否构成一个环 注意到构成一个环,所有点的度数为2,即一个点只有两条边与之相连,再有就是判断合并之后这n个点是否在同一个连通块 Circle Time Limit: ...

  6. zoj3811 Untrusted Patrol (dfs)

    2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onl ...

  7. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  8. ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

    Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB    ...

  9. hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)

    Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. SecureCRT中vi或vim编辑器显示中文乱码问题

    vi ~/.vimrc  //新建文件 syntax on set showmode set autowrite set number set encoding=utf-8 fileencodings ...

  2. 【学习笔记】C++ cout 输出小数点后指定位数

    在C中我们可以使用 printf("%.2lf",a);但在C++中是没有格式操作符的,该如何操作: C++使用setprecision()函数,同时必须包含头文件iomanip, ...

  3. JS正则匹配待重命名文件名

    <script>var str = "123 - Copy(2).csv";var regExp = /^123( - Copy(\(\d+\))?)?.csv$/;d ...

  4. SAP CRM点了附件的超链接后报错的处理方式

    SAP CRM系统里,点击了附件的这些超链接后,如果是文本文件,会在浏览器里打开.如果是其他类型的文件,会弹出下载对话框. 然而最近我工作时遇到一个问题,点击超链接后,总是弹出Logon failed ...

  5. 10g集群启动顺序

    1. 首先, /etc/inittab(不同平台文件名可能不同),文件中的下面3行被调用. h1:35:respawn:/etc/init.d/init.evmd run >/dev/null ...

  6. Android主题更换换肤

    知识总览android主题换肤通常借助LayoutInflater#setFactory实现换肤. 换肤步骤: 通过解析外部的apk压缩文件,创建自定义的Resource对象去访问apk压缩文件的资源 ...

  7. .net MVC下跨域Ajax请求(JSONP)

    一.JSONP(JSON with Padding) 客户端: <script type="text/javascript"> function TestJsonp() ...

  8. JAVA中等待所有线程都执行结束(转2)

    场景: package com.java4all.mypoint; import java.util.concurrent.CountDownLatch; public class ThreadTes ...

  9. spring-3-AOP

    自定义注解类 1.定义注解类 package anno; import java.lang.annotation.ElementType; import java.lang.annotation.Re ...

  10. No-2.常用 Linux 命令的基本使用

    常用 Linux 命令的基本使用 01. 学习 Linux 终端命令的原因 Linux 刚面世时并没有图形界面,所有的操作全靠命令完成,如 磁盘操作.文件存取.目录操作.进程管理.文件权限 设定等 在 ...