传送门

题目大意:

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. 轮询、长轮询、长连接、socket连接、WebSocket

    轮询:客户端定时向服务器发送Ajax请求,服务器接到请求后马上返回响应信息并关闭连接. 优点:后端程序编写比较容易. 缺点:请求中有大半是无用,浪费带宽和服务器资源.(而每一次的 HTTP 请求和应答 ...

  2. Nginx 限制php解析、限制浏览器访问

    限制php解析 1.有时候会根据目录来限制php解析: location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*\.p ...

  3. CreateWindow创建无边框 可拉伸窗体

    createwindow 定义 HWND WINAPI CreateWindow( _In_opt_ LPCTSTR lpClassName, _In_opt_ LPCTSTR lpWindowNam ...

  4. MySQL性能调优思路

    1.MySQL性能调优思路 如果一台服务器出现长时间负载过高 /周期性负载过大,或偶尔卡住如何来处理? 是周期性的变化还是偶尔问题?是服务器整体性能的问题, 还是某单条语句的问题? 具体到单条语句, ...

  5. [CF]Cycling City

    题目大意:给定一张无向图,问图中是否存在两个点,使得这两个点之间有三条路径,而且三条路径没有公共点. 解法: 我们可以先走出来一个环,再出环上任意一点走到另外一点.就像这样:

  6. TP多条件sql查询,分组排序

    $k=M('order a'); $bj=$k->join("left join __CHANGE__ b on b.tb_name='order'and a.order_id=b.t ...

  7. DataStage系列教程 (Pivot_Enterprise 行列转换)

    有人提到Pivot_Enterprise这个组件,之前没有用过,今天捣腾了会,写下来供以后参考,如果有什么不对的,还请多指出,谢谢! Pivot_Enterprise主要用来进行行列转换. 1 示例 ...

  8. Pyspider爬虫教程

    Pyspider爬虫教程 一.安装 1. 安装pip (1)准备工作 yum install –y make gcc-c++ python-devel libxml2-devel libxslt-de ...

  9. vue-cli 一分钟搭建自己的vue项目

    创建项目 1.安装全局vue-cli npm install vue-cli -g 2.生成项目模板(my_projuct为项目名称) vue init webpack my_projuct 3.进入 ...

  10. scons的使用

    以下测试是在linux下. 1.安装. $sudo apt install scons 2.查看安装版本: $scons --version 会出现以下内容: SCons by Steven Knig ...