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的更多相关文章

  1. ZOJ 3811 Untrusted Patrol

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811 解题报告:一个无向图上有n个点和m条边,其中有k个点上安装 ...

  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 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 ...

  4. ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

    Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB    ...

  5. ZOJ 3811 Untrusted Patrol【并查集】

    题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点 思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序WA了几发, ...

  6. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  7. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  8. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  9. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

随机推荐

  1. Firefox火狐Flash插件卡死问题完美解决方法(转载)

    http://www.ihacksoft.com/firefox-flash-protectedmode.html 其实这个问题以前就出现过,而最近该问题又出现在最新的 Windows 8.1 系统中 ...

  2. linux win 的换行转换

    Linux和Windows和换行符不一样.Windows下是CRLF(\r\n或0d0a),Linux下是LF(\n或0a).在Linux下有时会遇到从Windows过来的文本文件,这些文件带了Win ...

  3. Spring MVC Controller中解析GET方式的中文参数会乱码的问题(tomcat如何解码)

    Spring MVC Controller中解析GET方式的中文参数会乱码的问题 问题描述 在工作上使用突然出现从get获取中文参数乱码(新装机器,tomcat重新下载和配置),查了半天终于找到解决办 ...

  4. 一步一步配置NLB(续)之深入测试

    在这里http://blog.csdn.net/haoxiaozigang1/article/details/12198679跟大家分享了NLB配置的过程,下面写一些对NLB不同情况的下测试的结果: ...

  5. WCF服务编程中使用SvcMap实现类型共享等技巧【转】

    原文链接:http://www.cr173.com/html/19026_1.html 国外的一篇文章:Sharing DataContracts between WCF Services 文中提到的 ...

  6. android webview web里面的数据透传到java以及java的数据透传到web

    详见: http://tutorials.jenkov.com/android/android-web-apps-using-android-webview.html#android-web-app- ...

  7. 在 CentOS 7 中安装并使用自动化工具 Ansible

    Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于Chef和Puppet,但是有一个不同和优点是我们不需要在节点中安装任何客户端.它使用SSH来和节点进 ...

  8. SQL远程创建数据库

    CREATE PROCEDURE [dbo].[p_CreateDB]   @Des_DB sysname,  @ServerName sysname=N'',  @UserName sysname= ...

  9. redis sort

    redis sort命令用法 1.命令描述返回或保存给定列表.集合.有序集合key中经过排序的元素.排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较. 2.一般sort用法最简单的sort ...

  10. jquery 字数限制

    $("#TextArea1").keydown(function(){ 10 var curLength=$("#TextArea1").val().lengt ...