洛谷 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 ...
随机推荐
- Ubuntu 14.04上架IPSec+L2TP的方法
最简单的方法是使用脚本一步一步地进行配置.我用的是philplckthun写的脚本,修改了一下获取服务器IP的方法:脚本文件. 在ubuntu下运行: sh setup.sh 配置配置完成后,服务器端 ...
- 添加一个vue全局守卫,主要用于用户登录时候验证
//注册一个全局守卫,作用是在路由跳转钱,对路由进行判断,防止未登录用户跳转到其他页面 router.beforeEach((to, from, next) => { let token = l ...
- RN项目中缩进处理
SpannableString使用详解http://blog.csdn.net/u012702547/article/details/49895157 Spannable的用法http://www.j ...
- React Native 网络请求封装:使用Promise封装fetch请求
最近公司使用React作为前端框架,使用了异步请求访问,这里做下总结: React Native中虽然也内置了XMLHttpRequest 网络请求API(也就是俗称的ajax),但XMLHttpRe ...
- LeetCode——Find the Difference
LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...
- golang learning
开发用py和go 入职前学了几天py入职后看的代码也是py 现在终于还是要学go了 初体验:感觉和c py都很像 入门语法看起来很简单的样子 学了py之后现在各种随意 需要多注意 在函数传参的时候 c ...
- DPDK之(八)——vhost库
转:http://www.cnblogs.com/danxi/p/6652725.html vhost库实现了一个用户空间的virtio net server,允许用户直接处理virtio ring队 ...
- ubuntu 编译 vim+lua
mac $ brew install vim --with-cscope --with-lua --override-system-vim 安装spf13-vim见下面linux部分. 如果安装mac ...
- 在启动mysql的时候出现如下问题:“ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)”
今天在启动Mysql 的时候出现如下的问题:“ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)” 在查询 ...
- Stylus的基础用法
介绍 在学习一个 Vue.js 项目的过程中,注意到源码中样式的部分并没有用熟悉的 .css 样式文件,而是发现了代码长得和 CSS 相像的 .styl 文件.这个 .styl 以前没见过啊,你是谁? ...