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 ...
随机推荐
- 【转】Session ID/session token 及和cookie区别
Session + Cookie 知识收集! cookie机制采用的是在客户端保持状态的方案.它是在用户端的会话状态的存贮机制,他需要用户打开客户端的cookie支持.cookie的作用就是为了解决 ...
- PHP——字符串统一转码为GBK,自动判断是否UTF8并转码
public static function strToGBK($strText) { $encode = mb_detect_encoding($strText, array('UTF-8','GB ...
- 在SQLite中创建数据库时总是提示错误?
答案:原先以为是因为编码影响的其实不是,是因为逗号和分号的原因,不是标准的英文状态下的格式
- C#入门篇6-6:字符串操作 StringBiulder string char[]之间的转化
//StringBiulder string char[]之间的转化 public static void Fun3() { StringBuilder sb = new StringBuilder( ...
- shell学习记录001-知识点储备
1.BASH(bourne again shell ) cmd1 ;cmd2等同于 cmd1 cmd2 2.echo music; 中的分号不被打印出,因为分号默认为命令定界符号 3.利用pgrep找 ...
- java面向对象编程—— 第三章 程序流程控制
3.1流程控制 三种基本技术可以改变程序的控制流程: ① 调用方法:调用方法将导致控制流程离开当前方法,转移到被调用的方法: ② 选择:java中有两种做出选择的机制:if/else语句和sw ...
- lazyload 分页加载
http://www.neoease.com/lazy-load-jquery-plugin-delay-load-image/ echo $d['pic']; ?>" src=&qu ...
- 关于HashMap中的负载因子
这两天在看HashMap的时候,被负载因子float loadFactor搞得很晕,经过一天的研究,最后理出了自己的一点个人见解. 在HashMap的底层存在着一个名字为table的Entry数组,在 ...
- vim的编码设置
VIM的相关字符编码主要有三个参数 fencs: 它是一个编码格式的猜测列表.当用vim打开某个文件时,会依次取这里面的编码进行解码,如果某个编码格式从头至尾解码正确,那么就用那个编码 fenc:它是 ...
- windows命令行及批处理文件小结
1.命令Shell概述(Command shell overview): The command shell is a separate software program that provides ...