ZOJ 3811
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5343
网络赛这水题没写过太伤了,赛后写了下1A。
当时钻牛角尖一定要用k次bfs,其实一次就够了,把扩展到的节点插入set中,复杂度nlogn
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
using namespace std ; vector <int> mp[] ;
set <int> s ;
int idx[] ;
int vis[] ;
int node[] ;
int find(int x)
{
return idx[x]==x?x:idx[x]=find(idx[x]) ;
} int n,m,k ;
int flag[] ; int main()
{
int T ;
scanf("%d",&T) ;
while(T--)
{
scanf("%d%d%d",&n,&m,&k) ;
memset(flag,,sizeof(flag)) ;
for(int i= ;i< ;i++)
mp[i].clear() ;
s.clear() ;
memset(vis,,sizeof(vis)) ;
for(int i= ;i<=n ;i++)
idx[i]=i ;
for(int i= ;i<k ;i++)
{
int x ;
scanf("%d",&x) ;
flag[x]= ;
}
int ff= ;
while(m--)
{
int a,b ;
scanf("%d%d",&a,&b) ;
mp[a].push_back(b) ;
mp[b].push_back(a) ;
int p=find(a) ;
int q=find(b) ;
if(p!=q)
idx[p]=q ;
}
for(int i= ;i<=n ;i++)
{
if(find(idx[])!=find(idx[i]))
{
ff= ;
break ;
}
}
int L ;
scanf("%d",&L) ;
if(L!=k)ff= ;
for(int i= ;i<L ;i++)
scanf("%d",&node[i]) ;
queue <int> q ;
s.insert(node[]) ;
for(int j= ;j<L ;j++)
{
if(s.find(node[j])!=s.end())
{
q.push(node[j]) ;
vis[node[j]]= ;
while(!q.empty())
{
int u=q.front() ;
q.pop() ;
for(int i= ;i<mp[u].size() ;i++)
{
if(!vis[mp[u][i]])
{
vis[mp[u][i]]= ;
if(flag[mp[u][i]])
{
s.insert(mp[u][i]) ;
}
else
{
q.push(mp[u][i]) ;
}
}
}
}
continue ;
}
else
{
ff= ;
break ;
}
}
if(ff)puts("Yes") ;
else puts("No") ;
}
return ;
}
ZOJ 3811的更多相关文章
- ZOJ 3811 Untrusted Patrol
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811 解题报告:一个无向图上有n个点和m条边,其中有k个点上安装 ...
- 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 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 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集
Untrusted Patrol Time Limit: 3 Seconds Memory Limit: 65536 KB ...
- ZOJ 3811 Untrusted Patrol【并查集】
题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点 思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序WA了几发, ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
随机推荐
- 转 数据库中的 date datetime timestamp的区别
转 数据库中的 date datetime timestamp的区别 DATETIME, DATE和TIMESTAMP类型是相关的.本文描述他们的特征,他们是如何类似的而又不同的. DATETIME类 ...
- eclipse中SVN分支合并到主干
在项目开发中,需要添加一些新的功能,但是又不想影响到其他开发人员的项目进度,所以决定使用SVN分支进行开发,分支开发完毕后再合并到主干.本文介绍如何在eclipse中合并分支到主干. 1. 要想将分支 ...
- A-Apple Catching(POJ 2385)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8759 Accepted: 4264 De ...
- C#常用的加密算法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 方法一: //须添加对System.Web的引用 ...
- excel快捷键设置
Excel技能 按键 结果 序号 Alt+1 合并后居中 1 ALT+2 边框 2 ALT+3 边框线型 3 ALT+4 格式刷 4 ALT+5 清除格式 5 CTRL+SHIFT+P 设置字体 6 ...
- java并发带返回结果的批量任务执行
转载:http://www.it165.net/pro/html/201405/14551.html 一般情况下,我们使用Runnable作为基本的任务表示形式,但是Runnable是一种有很大局限的 ...
- Excel中连接函数CONCATENATE()
直接API: CONCATENATE 函数语法具有下列参数 (参数:为操作.事件.方法.属性.函数或过程提供信息的值.): Text1 必需. 要连接的第一个文本项. Text2, ... 可选. 其 ...
- (转)mysql的增删改查
MySQL数据库的增删改查. 1,首先启动mysql数据库的服务,在运行的窗口中输入:net start mysql,这样,就可 以启动mysql数据库的服务,同理,输入net stop mysql, ...
- subString用法,字符串保持一定位数,不足补0
Substrinig(a,b): 从下标a开始截取,共截取b位 实现:一串数字,中间两位数字+2,生成新的一串数字 "; , number.Length - );//前8位 );//后6位 ...
- ubnutu安装sougou 输入法
先安百度经验安装fcitx 1.首先下载sogoupinyin_2.0.0.0068_amd64.deb,点击安装后,会通过ubuntu软件中心安装,安装玩成后,任然是无法使用.然后: 2.然后执行下 ...