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

思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序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. python中函数参数

    默认参数注意点 优点:灵活,当没有指定与形参对应的实参时就会使用默认参数 缺陷: 例子: >>> def h(m, l=[]):                    #默认参数时列 ...

  2. 找不到draw9patch.bat?已经不用找了

    Google 已经因为 draw9patch 热门的原因,把它集成在 Android Studio 里面了, 你现在可以直接在 Android Studio 里直接打开编辑了.

  3. python+selenium(环境的安装)

    前言:网上的资料层次不齐,且资料也不全,容易误导新手,所以笔者愿意把你的知识免费分享给大家,笔者用的版本为:python3 此时可能新手就会问了,为什么不用python2呢,因为道理很简单,人要往前走 ...

  4. 如何破解密码的哈希值,破解双MD5密码值

    这是关于我如何破解密码的哈希值1亿2200万* John the Ripper和oclHashcat-plus故事. 这是几个月前,当我看到一条推特:从korelogic约含共1亿4600万个密码的密 ...

  5. python 网络编程篇

    基础模拟通话网络程序: #客户端 import socket client = socket.socket() client.connect(('localhost',6969)) client.se ...

  6. Vue之computed与watch的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. \p{space}|\p{Hex}|\p{Digit}|转译符|\1|\g{-1}|[^ab]|/([^\d\D]+)/

    小骆驼 第七章 漫游正则表达式王国 #!/usr/bin/perl use strict; use warnings; $_ = 'ab \ cde f ghijk10.x12ln'; if(/d/) ...

  8. VS2015提示:未安装Style的Visual Studio语言支持,代码编辑Intellisense将不可用。服务器控件的标记Intellisense可能不起作用

    一.问题 最近在VS2015打开文件,提示未安装Style的Visual Studio语言支持,代码编辑Intellisense将不可用.服务器控件的标记Intellisense可能不起作用. Int ...

  9. 169. Majority Element@python

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  10. JavaScript设计模式基础之this、call、apply

    1.this的指向 除去不常用的with和eval,具体应用中this指向大概能分为4种情况分别是 1.作为对象的方法调用. 2.作为普通函数的方法调用. 3.Function.prototype.c ...