传送门

题目大意:

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. div 文章内容自动分屏显示

    <head runat="server"> <title></title> <script language="javascri ...

  2. Python MySQL数据库连接模块

    1. MySQLdb只支持在Python 2版本使用MySQLdb是用于Python链接Mysql数据库的接口.a.pip安装 直接使用pip进行安装,在此之前需要安装一些系统依赖包. ● CentO ...

  3. os包方法

    os包中实现了平台无关的接口,设计向Unix风格,但是错误处理是go风格,当os包使用时,如果失败之后返回错误类型而不是错误数量. os包中函数设计方式和Unix类似,下面来看一下. func Chd ...

  4. grable编译spring源码并导入eclipse

    1.下载安装gradle, spring 源码构建加入了gradle支持. gradle下载: http://www.gradle.org/downloads ,下载后设置环境变量: GRADLE_H ...

  5. Autofac is designed to track and dispose of resources for you.

    https://autofaccn.readthedocs.io/en/latest/best-practices/ Autofac is designed to track and dispose ...

  6. Java数据类型——面试题

    1.short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 +=1;有什么错? 答:对于short s1=1;s1=s1+1来说,在s1+1运算时会自动提升表 ...

  7. lua笔记之userdata

    1.一直使用框架里封装好的c库,想着自己一点一点的写些例子,学习下,以后需要c库,可以自己写了. 下边是一个简单的userdata的例子--数组操作. newarray.c #include &quo ...

  8. C#470多例winform 界面特效的源码

    一共470多例winform 界面特效的源码. 窗体与界面设计... 9 实例001  带历史信息的菜单    10 实例002  菜单动态合并    12 实例003  像开始菜单一样漂亮的菜单.. ...

  9. JQuery -- Validate, Jquery 表单校验

    1. Jquery 表单验证需要插件 jQuery validation 1.7  ---验证插件需要:jQuery 1.3.2 或 1.4.2版本 http://jquery.bassistance ...

  10. [ SSH 两种验证方式原理 ]

    SSH登录方式主要分为两种: 1. 用户名密码验证方式 说明: (1) 当客户端发起ssh请求,服务器会把自己的公钥发送给用户: (2) 用户会根据服务器发来的公钥对密码进行加密: (3) 加密后的信 ...