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

    http://jsonviewer.codeplex.com/ jsoneditor https://github.com/josdejong/jsoneditor

  2. 报错总结_java.lang.RuntimeException: Invalid action class configuration that references an unknown class name

    在使用SSH进行项目开发时,一不小心就可能出现以上的错误提示. 这样的问题可以简单理解为未找到名字为XXX的action 1)xxxAction没有在Struts.xml中配置相应的action: 大 ...

  3. 时间序列数据库选型——本质是列存储,B-tree索引,抑或是搜索引擎中的倒排索引

    时间序列数据库最多,使用也最广泛.一般人们谈论时间序列数据库的时候指代的就是这一类存储.按照底层技术不同可以划分为三类. 直接基于文件的简单存储:RRD Tool,Graphite Whisper.这 ...

  4. Java 生成压缩包,ZipOutputStream的使用

    案例:根据url 获取网络资源A,B,C   将资源A,B,C放在一起生成一个xxx.zip 直接看代码 import java.io.File; import java.io.FileOutputS ...

  5. 为不同浏览器创建XMLHttpRequest对象

    /*声明一个XMLHttpRequest对象*/ var xmlHttp=false; /*@cc_on@*/ /*if(@_jscript_version>=5) try { xmlHttp= ...

  6. shell脚本初析

    简单理解:运用很多工具,将复杂的步骤简单化,体现在shell脚本中框架:必须有备注,写的别人能够看得懂开头:#! /bin/bash 代表的意思是改文件使用的是bash语法要想使用该种方法运行shel ...

  7. 快速配置 Samba 将 Linux 目录映射为 Windows 驱动器,用于跨平台编程

    一.局域网内的 Linux 服务器上操作步骤: 1.安装samba(CentOS Linux): yum install samba system-config-samba samba-client ...

  8. Linux 下一个很棒的命令行工具

    导读 Taskwarrior 是 Ubuntu/Linux 下一个简单而直接的基于命令行的 TODO 工具.这个开源软件是我曾用过的最简单的基于命令行的工具之一.Taskwarrior 可以帮助你更好 ...

  9. bzoj 2132: 圈地计划

    #include<cstdio> #include<iostream> #include<cstring> #define M 100009 #define inf ...

  10. 关于Linux下C编译错误(警告)cast from 'void*' to 'int' loses precision

    char *ptr; //此后省略部分代码 ) //出错地方 那句话的意思是从 void* 到 int 的转换丢失精度,相信看到解释有些人就明白了, 此问题只会出现在X64位的Linux上,因为在64 ...