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 ...
随机推荐
- Keepalived+LVS(DR)+MySQL
实验环境 主机名 IP VIP 服务 主备 KA_LV_MYSQL_01 192.168.30.130 192.168.30.100 keepalived.LVS.MySQL MASTER KA_LV ...
- 服务器php-cgi.exe进程过多,导致CPU占用100%的解决方法
再使用iis服务器中经常会出现php-cgi.exe进程过多,导致CPU占用100%,最终造成网站运行过慢甚至卡死的情况,重启iis会好一会,过一段时间久出现这种情况,为什么会出现这种情况呢,应该怎么 ...
- AJPFX关于Swing组件的总结
默认布局管理器是流式布局(FlowLayout) 按钮的建立: jb1=new JButton("香蕉") 面板的建立:jp1=new JPanel(); 设置JFrame的标题: ...
- BBS项目总结
数据库(Oracle): BBSUserid:主键username:用户名password:密码pic:头像 blobpagenum:每个人分页喜好数量,每页显示多少行 Article :ID:主键, ...
- H+后台主题UI框架---整理(三)
这里面介绍下H+后台主题UI框架里面插件的应用,不过都是最最简单最初级的功能.主要有日历插件,input单选多选(icheck)插件,input下拉搜索(chosen)插件. 一.日历插件 有如下几种 ...
- 找不到draw9patch.bat?已经不用找了
Google 已经因为 draw9patch 热门的原因,把它集成在 Android Studio 里面了, 你现在可以直接在 Android Studio 里直接打开编辑了.
- 合并百度影音的离线数据 with python 2.3 格式更新
很久没有更新了. 这次新增支持四种格式的解析. filelist slicelist download.cfg third_party_download.cfg 还是2个文件.替换之前版本即可. 初步 ...
- java异常处理中的细节
首先看一段代码 public class Test{ public static String output=""; public static void foo(int i){ ...
- C# 递归读取XML菜单数据
在博客园注册了有4年了,很遗憾至今仍未发表过博客,趁周末有空发表第一篇博客.小生不才,在此献丑了! 最近在研究一些关于C#的一些技术,纵观之前的开发项目的经验,做系统时显示系统菜单的功能总是喜欢把数据 ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...