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 ...
随机推荐
- 使用js给页面显示的图片添加水印效果
功能描述:使用Jquery 给页面的图片添加 版权信息水印. 这里的水印并不是真的把每一张图片上都添加了水印.而是在图片的上方添加了一个层,层中包含了水印图片效果就像是图片上加了水印. 功能原理:1, ...
- [转]Windows的窗口刷新机制
1.Windows的窗口刷新管理 窗口句柄(HWND)都是由操作系统内核管理的,系统内部有一个z-order序列,记录着当前窗口从屏幕底部(假象的从屏幕到眼睛的方向),到屏幕最高层的一个窗口句柄的排序 ...
- PHP后台
一.ajax提交表单 先引入ajax.js function ajax(url, fnSucc, fnFaild) { //1.创建Ajax对象 var oAjax=null; if(window.X ...
- VS2010连接SQLite数据库
Visual studio 2010及以上版本,连接SQLite数据库 1.在Sqlite开发站点下载SQLite的.exe安装包 Ctrl+F搜索这条语句:This is the only setu ...
- long long 读数scanf的转换 #define
在win32的评测系统下,long long scanf 要用"%I64d" ,而网上评测和考试要用"%lld",因此,难免有点麻烦,还会runtime err ...
- sql遍历树
oracle有直接的sql来遍历一颗树的子节点和父节点 遍历一个节点的所有子节点(classid的值就是该节点的值) select * from organization_ a start with ...
- 事件函数SetEvent、PulseEvent与WaitForSingleObject详解
系统核心对象中的Event事件对象,在进程.线程间同步的时候是比较常用,发现它有两个出发函数,一个是SetEvent,还有一个PulseEvent, 两者的区别是: SetEvent为设置事件对象为有 ...
- Oracle中varchar,varchar2,nvarchar,nvarchar2的区别
--varchar,varchar2 联系:1.varchar/varchar2用于存储可变长度的字符串比如varchar(20),存入字符串'abc',则数据库中该字段只占3个字节,而不是20个字节 ...
- AndroidStudio导入第三方开源库 --文件夹源码
1 在已打开的项目中 File-New-ImportModule 选择开源项目中的 库所在文件夹比如 library文件夹 然后导入. 2 File-Project Sructure 在Modu ...
- C# HttpBrowser 跨进程访问,解决内存泄露问题
#undef DEBUG using Microsoft.Win32; using Newtonsoft.Json; using System; using System.Collections.Ge ...