洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
题目大意:
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的更多相关文章
- 洛谷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 ...
- 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm
农夫约翰和他的奶牛准备去旅行,所以约翰想要把他的农场临时关闭. 农场有N个牛棚(牛棚从1到N编号),有M条路连接这些牛棚(1≤N,M≤3000). 约翰打算挨个关闭牛棚,在关牛棚的时候, 他突然想起一 ...
- [USACO16OPEN]关闭农场Closing the Farm_Silver
题目描述 FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱. 这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000).为了关闭整 ...
- [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 ...
- P3144 [USACO16OPEN]关闭农场——离线,并查集
https://www.luogu.org/problem/P3144 每次关闭一个农场,农场之间有边相连,问每次关闭后开着的农场是否是一个连通块: 数据小,离线搞: 我们先记录删的顺序,然后倒着来, ...
- 一道并查集的(坑)题:关闭农场closing the farm
题目描述 in English: Farmer John and his cows are planning to leave town for a long vacation, and so FJ ...
- 洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
随机推荐
- PHP 网站隔离配置
PHP网站间隔离 网站内目录与目录之间是可以访问的,在某些特定情况下这样是不安全的,如果目录间网址权限被黑客利用很可能造成数据流失,在这里我们可以通过PHPopen_basedir来实现网站间目录隔离 ...
- lamp架构之升级php版本
当你看到这篇文章的时候 YHSPY.COM 服务器上的PHP版本已经从 5.4.27 升级到了 7.0.4,这是一个重大的飞跃.一路升级遇到了很多问题.官方声称PHP7最大的升级就是在语言性能上的提升 ...
- springJDBC的几种方法
1.简单粗暴,直接在类中创建连接池使用 package com.xiaostudy; import org.apache.commons.dbcp.BasicDataSource; import or ...
- rhel yum报错
用yum报错 http://mirrors.aliyun.com/centos/%24releasever/addons/x86_64/repodata/repomd.xml: [Errno 12] ...
- Sqrt(X),求平方根,折半查找
算法分析:利用折半查找,降低算法复杂度.前面求x得y次幂,也是将y/2,都是为了降低复杂度. //折半查找的思想 public class Sqrt { public int sqrt(int x) ...
- RabbitMQ初体验
这里官方使用的Pom是4.0.2版本 <dependencies> <dependency> <groupId>com.rabbitmq</groupId&g ...
- 负载均衡之HTTP重定向
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38014581 由于目前现有网络的各个核心部分随着业务量的提高,访问量和数据流量的快速增长,其 ...
- Android DDMS ADB启动失败错误解决!
ADB server didn't ACK && make sure the plugin is properly configured! adb启动失败一般是端口被占用! 解决方法和 ...
- eclipse导入android项目错误,项目名称上有红叉,但代码中无报错
首先,在项目上右键,属性,选择编译目标,选择android版本或把Android的版本调高一点.
- 常见CSS浏览器兼容性问题与解决方案【转载自http://blog.csdn.net/chuyuqing/article/details/37561313/】
所谓的浏览器兼容性问题,是指因为不同的浏览器对同一段代码有不同的解析,造成页面显示效果不统一的情况.在大多数情况下,我们的需求是,无论用户用什么浏览器来查看我们的网站或者登陆我们的系统,都应该是统一的 ...