https://www.luogu.org/problem/P3144

每次关闭一个农场,农场之间有边相连,问每次关闭后开着的农场是否是一个连通块;

数据小,离线搞;

我们先记录删的顺序,然后倒着来,先将所有删去的点都标记,每次加点,再把所有没删的都加在一起,

in_class[i]==1表示这个点在农场里;

我们从一个删点的过程变成一个加点的过程;

因为在第n次点都删完了,所以一定是一个连通块(体积为0);

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int n,m;
int father[maxn];
int xx[maxn],yy[maxn];
int getfather(int x)
{
if(father[x]==x) return x;
father[x]=getfather(father[x]);
return father[x];
}
int in_class[maxn];
int delete_order[maxn]; void merge(int x,int y)
{
int fx=getfather(x);
int fy=getfather(y);
if(father[fx]!=fy)
{
father[fx]=fy;
}
}
int num_block[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&xx[i],&yy[i]);
}
for(int i=;i<=n;i++)
{
int w;
scanf("%d",&w);
delete_order[i]=w;
in_class[w]=;
father[i]=i;
}
for(int i=n;i>=;i--)
{
in_class[delete_order[i]]=;
for(int j=;j<=m;j++)
{
if(in_class[xx[j]]==&&in_class[yy[j]]==)
{
merge(xx[j],yy[j]);
}
}
for(int j=;j<=n;j++)
{
if(in_class[j]==&&getfather(j)==j)
{
num_block[i]++;
}
}
}
for(int i=;i<n;i++)
{
if(num_block[i]==) printf("YES\n");
else printf("NO\n");
}
printf("YES\n");
return ;
}

P3144 [USACO16OPEN]关闭农场——离线,并查集的更多相关文章

  1. 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

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

  2. 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm

    农夫约翰和他的奶牛准备去旅行,所以约翰想要把他的农场临时关闭. 农场有N个牛棚(牛棚从1到N编号),有M条路连接这些牛棚(1≤N,M≤3000). 约翰打算挨个关闭牛棚,在关牛棚的时候, 他突然想起一 ...

  3. 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

    传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...

  4. [bzoj1015](JSOI2008)星球大战 starwar(离线+并查集)

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武 器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通 ...

  5. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

  6. HDU5441 Travel 离线并查集

    Travel Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, ...

  7. [USACO18FEB] Snow Boots G (离线+并查集)

    题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...

  8. [USACO16OPEN]关闭农场Closing the Farm_Silver

    题目描述 FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱. 这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000).为了关闭整 ...

  9. 【杭电OJ3938】【离线+并查集】

    http://acm.hdu.edu.cn/showproblem.php?pid=3938 Portal Time Limit: 2000/1000 MS (Java/Others)    Memo ...

随机推荐

  1. 怎样获取全局对象 window

    1. 使用window.self window.self === window; // true 2. 使用window.window window.window === window; // tru ...

  2. string.Format 格式化

    1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...

  3. Python Web 程序使用 uWSGI 部署

    Python Web 程序使用 uWSGI 部署 WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway ...

  4. C++ 基础知识汇总 持续更新

    摘录一些C++面试常考问题,写一些自己的理解,花了挺长时间的,作图是真的累,欢迎来摘果子. static关键字 用于声明静态对象: 静态函数只在本文件可见.(默认是extern的) 全局静态对象:全局 ...

  5. lumen添加自定义异常

    在公用工具类写异常类 <?php namespace Brady\Tool\Exception; use Brady\Tool\Constant\ErrorMsg; use \Exception ...

  6. Python面向对象之多态、封装

    一.多态 超过一个子类继承父类,出现了多种的形态. 例如,动物种类出现了多种形态,比如猫.狗.猪 class Animal:pass class Cat(Animal):pass class Dog( ...

  7. IAR建立stm32工程

    stm32是一个当下非常流行的微控制器,很多人都加入了学习stm32的行列中,常用的stm32编译器有IAR和mdk两种,接下来是利用stm32固件库3.5在IAR下的建立的工程模板历程: 1.在常用 ...

  8. Python语言程序设计:Lab4

    Programming 1.Analysing a Text File Look at the file xian_info.txt which is like this: Xi'an China 8 ...

  9. Run Code Once on First Load (Concurrency Safe)

    原文: https://golangcode.com/run-code-once-with-sync/ ------------------------------------------------ ...

  10. java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver

    SpringBoot运行报错——java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or rep ...