BZOJ 4579: [Usaco2016 Open]Closing the Farm
Description
依次删去一个点和它的边,问当前图是否连通.
Sol
并查集.
倒着做就可以了.
每次将一个点及其的边加入,如果当前集合个数大于 1,那么就不连通.
Code
/**************************************************************
Problem: 4579
User: BeiYu
Language: C++
Result: Accepted
Time:2196 ms
Memory:10328 kb
****************************************************************/ #include <cstdio>
#include <vector>
#include <iostream>
using namespace std; const int N = 200050; int n,m,cs;
int b[N],p[N],f[N],ans[N];
vector< int > g[N]; inline int in(int x=0){ scanf("%d",&x);return x; }
int find(int x){ return f[x] == x ? x : f[x]=find(f[x]); }
int main(){
n=in(),m=in();
for(int i=1,u,v;i<=m;i++){
u=in(),v=in();
g[u].push_back(v),g[v].push_back(u);
}
for(int i=1;i<=n;i++) p[i]=in();
for(int i=1;i<=n;i++) f[i]=i; for(int x=n,u,v;x;--x){
cs++;
u=p[x],b[u]=1;
for(int i=0,lim=g[u].size();i<lim;i++){
v=g[u][i];
if(b[v]) if(find(u) != find(v)) f[find(u)]=find(v),cs--;
}
if(cs > 1) ans[x]=0;else ans[x]=1;
}
for(int i=1;i<=n;i++) if(ans[i]) puts("YES");else puts("NO");
return 0;
}
BZOJ 4579: [Usaco2016 Open]Closing the Farm的更多相关文章
- 【bzoj4579】[Usaco2016 Open]Closing the Farm 并查集
题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...
- BZOJ 4576: [Usaco2016 Open]262144
Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco ...
- 【BZOJ 4579】【Usaco2016 Open】Closing the Farm
http://www.lydsy.com/JudgeOnline/problem.php?id=4579 把时间倒过来,只是加点,并查集维护连通块. #include<cstdio> #i ...
- 续并查集学习笔记——Closing the farm题解
在很多时候,并查集并不是一个完整的解题方法,而是一种思路. 通过以下题目来体会并查集逆向运用的思想. Description Farmer John and his cows are planning ...
- 一道并查集的(坑)题:关闭农场closing the farm
题目描述 in English: Farmer John and his cows are planning to leave town for a long vacation, and so FJ ...
- [USACO16OPEN]关闭农场Closing the Farm(洛谷 3144)
题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...
- BZOJ 4742: [Usaco2016 Dec]Team Building
4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 21 Solved: 16[Su ...
- BZOJ 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场
题目 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 491 S ...
- bzoj 4506: [Usaco2016 Jan]Fort Moo
4506: [Usaco2016 Jan]Fort Moo Description Bessie is building a fort with her friend Elsie. Like any ...
随机推荐
- k-nearest-neighbor算法
1. kNN 1.1 基本的kNN模型 kNN(k-nearest neighbor)的思想简单来说就是,要评价一个未知的东西U,只需找k个与U相似的已知的东西,并通过k个已知的,对U进行评价.假如要 ...
- vm centos 添加网卡 无配置文件
vm centos 添加网卡 无配置文件 解决办法 [root@test ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:C8:41:FB ...
- 药企信息sop
中国药品生产企业 http://db.yaozh.com/shengchanqiye 全球药品生产企业 http://db.yaozh.com/quanqiuqiye
- centos linux安装telnet 过程及问题(源于内部tomcat网站,外部无法访问)
首先本地没有telnet客户端及服务器 root权限下安装 yum install telnet yum install telnet-server vi /etc/xinetd.d/telnet 这 ...
- 《css3实战》读书笔记 第一章 基于CSS需求而编写的HTML.
笔记说明 <CSS3实战手册第3版(影印版)>可以消除Web设计工作的痛苦,并且带给你:HTML--重新入门.如果你是HTML新手,你会学到如何以CSS友好的方式进行基本页面构造.若你是H ...
- GLSL Debugger的姿势
https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/OpenGLShaderBuilder ...
- draw9的使用说明
转载来自:http://isux.tencent.com/android-ui-9-png.html 在Android的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压缩,这样就出现了可以 ...
- 【转】七年IT经验的七个总结
http://www.unitymanual.com/thread-30000-1-1.html?_dsign=ebe6a043 1.分享第一条经验:“学历代表过去.能力代表现在.学习力代表未来.” ...
- 如何让Chrome浏览器可以加载本地XML文件?
Chrome浏览器的安全限制,禁止本地加载XML等外部文件,如何设置让其可以加载呢? 有两种方法,第一种是在本地服务器环境下浏览,采用 http://localhost/ 的方式浏览你的网页和文件,就 ...
- php Hash Table(四) Hash Table添加和更新元素
HashTable添加和更新的函数: 有4个主要的函数用于插入和更新HashTable的数据: int zend_hash_add(HashTable *ht, char *arKey, uint n ...