传送门

题目大意:

n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是

否联通。

题解:并查集+倒序处理

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 3030
using namespace std; int n,m,sumedge,cnt; int head[N],fa[N],q[N],ans[N],exit[N]; struct Edge{
int x,y,nxt;
Edge(int x=,int y=,int nxt=):
x(x),y(y),nxt(nxt){}
}edge[N<<]; void add(int x,int y){
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
} int f(int x){
return fa[x]==x?x:fa[x]=f(fa[x]);
} int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
for(int i=;i<=n;i++)fa[i]=i;cnt=n;
for(int i=;i<=n;i++)scanf("%d",&q[i]);
for(int i=n;i>=;i--){
int x=q[i];exit[x]=true;
for(int h=head[x];h;h=edge[h].nxt){
int v=edge[h].y;
if(exit[v]==)continue;
int fx=f(x),fy=f(v);
if(fx!=fy){
fa[fx]=fy;
cnt--;
}
}
if(cnt==i)ans[i]=true;
}
for(int i=;i<=n;i++)
if(ans[i])puts("YES");
else puts("NO");
return ;
}

并查集

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 3009
using namespace std; int n,m,cnt; int q[N],exit[N],ans[N],d[N][N]; int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
d[x][y]=d[y][x]=true;
}
for(int i=;i<=n;i++)scanf("%d",&q[i]);
for(int i=n;i>=;i--){
bool flag=false;
int now=q[i];
exit[++cnt]=now;
for(int k=;k<=cnt;k++){
for(int j=;j<=cnt;j++){
for(int p=;p<=cnt;p++){
d[exit[j]][exit[p]]=d[exit[j]][exit[p]]||(d[exit[j]][exit[k]]&&d[exit[k]][exit[p]]);
}
}
}
for(int j=;j<=cnt;j++){
for(int p=j+;p<=cnt;p++){
if(d[exit[j]][exit[p]]==){
ans[i]=;flag=true;
break;
}
}
}
if(flag==false){
ans[i]=;
}
}
for(int i=;i<=n;i++)
if(ans[i])printf("YES\n");
else printf("NO\n");
return ;
}

50暴力

洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver的更多相关文章

  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. [USACO16OPEN]关闭农场Closing the Farm_Silver

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

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

  5. P3144 [USACO16OPEN]关闭农场——离线,并查集

    https://www.luogu.org/problem/P3144 每次关闭一个农场,农场之间有边相连,问每次关闭后开着的农场是否是一个连通块: 数据小,离线搞: 我们先记录删的顺序,然后倒着来, ...

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

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

  7. 洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...

  8. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  9. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

随机推荐

  1. qt项目: error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1900”不匹配值“1800”

    error LNK2038: 检测到“_MSC_VER”的不匹配项:  值“1900”不匹配值“1800” 该错误 网上通常的解释是: 原因:由于你使用了vs2012,相比较vs2010以及之前的vs ...

  2. Linux下Mysql的安装步骤

    (1).下载安装包 https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar [roo ...

  3. 本地存储(localStorage、sessionStorage、web Database)

    一.sessionStorage和localStorage sessionStorage和localStorage两种方法都是当用户在inPut文本框中输入内容并点击保存数据按钮时保存数据,点击读取数 ...

  4. java如何调用接口

    其实对于java调用接口进行获取对方服务器的数据在开发中特别常见,然而一些常用的基础的知识总是掌握不牢,让人容易忘记,写下来闲的时候看看,比回想总会好一些. 总体而言,一些东西知识点一直复制粘贴容易依 ...

  5. coredump调试小结

    在已经启动的进程中使用gdb,用gdb attach 查看so文件中的函数列表 nm -D *.so 关于c.c++类的gdb调试,强烈推荐一本书:debug hack

  6. java官网下载

    安装版 http://javadl.oracle.com/webapps/download/AutoDL?BundleId=234471_96a7b8442fe848ef90c96a2fad6ed6d ...

  7. Android中Fragment的Hide和Show

    我们都知道,Fragment动态添加的时候我们可以使用FragmentTransaction的add和replace方法,replace方法就等效于对这个Fragment先执行remove(),再执行 ...

  8. 换个思维,boot结合vue做后台管理

    可以添加,可以删除.动态的添加数据. 不用操作dom,只要操作json数据即可. <form class="form-horizontal addForm" id=" ...

  9. java-四则运算二

    1.实验目的:是否有乘除法,括号,多项式运算. 2.思路:利用简单的循环switch语句进行循环输出随机数 3.程序源代码: package jiajianchengchu; import java. ...

  10. Educational Codeforces Round 23D

    给n个数求每个子区间的价值,区间的价值是最大值-最小值 套路题= =,分别算最大值和最小值的贡献,用并查集维护,把相邻点连一条边,然后sort,求最大是按边价值(两个点的最大价值)小的排,求最小是按最 ...