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

思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序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. 基于ABP的Easyui admin framework正式开放源代码

    下载&反馈:http://www.webplus.org.cn v1.0 (2016/9/21) EF6+MVC5+API2+Easyui1.4.2开发 后台管理不使用iframe,全ajax ...

  2. SQL系列函数——字符串函数

    1.charindex函数用来寻找一个指定的字符(串)在另一个字符串中的起始位置,返回一个整数,没找到就返回0. select CHARINDEX('SQL','Microsoft SQL SERVE ...

  3. Java基础50题test3—水仙花数

    水仙花数 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例 如:153 是一个"水仙花数", ...

  4. Intro Of Myself

  5. 在Servlet中使用@Autowire的方法

    在你调用的Servlet中添加如下代码: public void init(ServletConfig config) { try { super.init(config); SpringBeanAu ...

  6. 接口自动化- 基于 Python

    准备工作 这部分其实在谷歌或者百度上搜索下就可以完成的,可是我就是想再啰嗦一遍,说不定有比我更懒的同学呢哈哈~ 第一步 Python的安装配置 打开官网: https://www.python.org ...

  7. 洛谷 U10223 Cx大帝远征埃及

    题目背景 众所周知,Cx是一个宇宙大犇.Cx能文善武,一直在为大一统的实现而努力奋斗着.Cx将调用他的精锐军队,一个精锐士兵最多可以战胜十个埃及士兵.同时Cx是个爱才的人,他想要制定一份能使在占领埃及 ...

  8. PYTHON PIP和kivy安装教程

    我们安装pip.我们同样需要在Python的官网上去下载 下载地址:https://pypi.python.org/pypi/pip 下载完成之后,解压到一个文件夹,用CMD控制台进入解压目录,输入: ...

  9. Smart Contracts

    A smart contract is a computer code running on top of a blockchain containing a set of rules under w ...

  10. DFS、BFS和Backtracking模板

    区别与联系 区别 DFS多用于连通性问题因为其运行思想与人脑的思维很相似,故解决连通性问题更自然,采用递归,编写简便(但我个人不这样觉得...) DFS的常数时间开销会较少.所以对于一些能用DFS就能 ...