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

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

  2. BZOJ 4576: [Usaco2016 Open]262144

    Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco ...

  3. 【BZOJ 4579】【Usaco2016 Open】Closing the Farm

    http://www.lydsy.com/JudgeOnline/problem.php?id=4579 把时间倒过来,只是加点,并查集维护连通块. #include<cstdio> #i ...

  4. 续并查集学习笔记——Closing the farm题解

    在很多时候,并查集并不是一个完整的解题方法,而是一种思路. 通过以下题目来体会并查集逆向运用的思想. Description Farmer John and his cows are planning ...

  5. 一道并查集的(坑)题:关闭农场closing the farm

    题目描述 in English: Farmer John and his cows are planning to leave town for a long vacation, and so FJ ...

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

  7. BZOJ 4742: [Usaco2016 Dec]Team Building

    4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 16[Su ...

  8. BZOJ 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场

    题目 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 491  S ...

  9. bzoj 4506: [Usaco2016 Jan]Fort Moo

    4506: [Usaco2016 Jan]Fort Moo Description Bessie is building a fort with her friend Elsie. Like any ...

随机推荐

  1. JavaWeb学习笔记——jsp:setproperty和getproperty

  2. Linux 普通进程 后台进程 守护进程

    一.普通进程与后台进程 默认情况下,进程是在前台运行的,这时就把shell给占据了,我们无法进行其它操作.对于那些没有交互的进程,很多时候,我们希望将其在后台启动,可以在启动参数的时候加一个'& ...

  3. php----显示中文乱码的问题

    条件: 在显示页面设置页面编码格式为<?php header('Content-Type: text/html; charset=utf-8');?>: 在写入数据库时设置:mysql_q ...

  4. 商城常用css分类代码

    如图: 原代码如下: <div class="allMerchan bgnone"> <h2 class="ttlm_category"> ...

  5. MySQL笔记-最简单的方法来解决找不到mysqld.sock文件的问题

    首先,环境:ubuntu 14.04,采用apt-get的方式安装的,手动安装可能路径设置稍有区别. 1.安装MySQL后,用命令行首次启动时发现找不到Mysqld.sock文件,提示: ERROR ...

  6. xss利用和检测平台

    xssing 是安全研究者Yaseng发起的一个基于 php+mysql的 网站 xss 利用与检测开源项目,可以对你的产品进行黑盒xss安全测试,可以兼容获取各种浏览器客户端的网站url,cooki ...

  7. safari 回调中window.open无法执行

    safari无法在callback中执行window.open,其安全机制将其阻挡了. 解决方法: var oWinRef = win.open('','_blank','参数');fCallback ...

  8. 使用proguard混淆java web项目代码

    1.首先下载proGuard.zip到本地: proguard4.5beta4.tar.zip解压开,2.新建文本文档,修改文件名为XXX.pro,然后复制下面内容到.pro -injars 'Y:\ ...

  9. php生成唯一随机码

    最终使用: echo md5(time() . mt_rand(1,1000000)) //A:利用时间戳的方法 md5("admin"); // B:32位MD5加密 subst ...

  10. 【转】使用Eclipse构建Maven项目 (step-by-step)

    安装eclipse 及配置maven时,参考的资料!!! from:http://blog.csdn.net/qjyong/article/details/9098213 Maven这个个项目管理和构 ...