ZOJ 3811 Untrusted Patrol【并查集】
题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点
思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序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【并查集】的更多相关文章
- 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 ...
- 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 ...
- ZOJ 3811 Untrusted Patrol
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811 解题报告:一个无向图上有n个点和m条边,其中有k个点上安装 ...
- ZOJ:2833 Friendship(并查集+哈希)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2833 A friend is like a flower, a rose ...
- ZOJ 3321 Circle【并查集】
解题思路:给定n个点,m条边,判断是否构成一个环 注意到构成一个环,所有点的度数为2,即一个点只有两条边与之相连,再有就是判断合并之后这n个点是否在同一个连通块 Circle Time Limit: ...
- zoj3811 Untrusted Patrol (dfs)
2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onl ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集
Untrusted Patrol Time Limit: 3 Seconds Memory Limit: 65536 KB ...
- 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 ...
随机推荐
- JDBC——入门知识【转】
1. 什么是JDBC:Java数据库连接性(JavaDatabase Connectivity) API,允许用户从Java应用程序中访问任何表格化数据源. 2. JDBC除了提供到更宽范围的SQ ...
- nginx 80端口重定向到443端口
server { listen ; server_name www.域名.com; rewrite ^(.*)$ https://${server_name}$1 permanent; } serve ...
- 新建cordova应用,插件开发教程系列(总目录)
以下几篇是连续的教程,代码也是连续的,包括如下章节: 新建cordova应用 https://www.cnblogs.com/cannel/p/11074359.html 使用cordova把h5应用 ...
- 安卓&IOS 手机添加O365 邮箱账户
手机添加O365 邮件账户 一.Android手机添加O365邮件账户 1. 找到手机上“电子邮件” 2. 打开设置 3. 点击添加账户 4. 选择“Exchange” 5. 输入O365的邮箱账户和 ...
- COGS 2111. [NOIP2015普及]扫雷游戏
★ 输入文件:2015mine.in 输出文件:2015mine.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 扫雷游戏是一款十分经典的单机小游戏.在 n 行 ...
- Windows下使用python库 curses遇到错误消息的解决方案
在Windows系统下执行python应用时,有时会遇到错误信息: ModuleNotFoundError: No module named '_curses'. 然而查看Windows系统里pyth ...
- Uniform Resource Identifier
https://en.wikipedia.org/wiki/Uniform_Resource_Identifier "URI" redirects here. For othe ...
- js 逻辑运算符、等号运算符
1 逻辑运算符 逻辑运算只有2个结果,一个为true,一个为false. 1.且&& ★ 两个表达式为true的时候,结果为true. ------------------------ ...
- vue props 下有验证器 validator 验证数据返回true false后,false给default值
vue props 下有验证器 validator 验证数据返回true false后,false给default值 props: { type: { validator (value) { retu ...
- Vector(同步)和ArrayList(异步)异同
//1 同步 异步 //未响应 = 假死 占用内存过多 内存无法进行处理 //请求的方式 :同步 异步 //网页的展现过程中(同步):1. css文件的下载 2.js文件的下载 3.下载你当前的htm ...