洛谷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 temporarily close down his farm to save money in the meantime.
The farm consists of NN barns connected with MM bidirectional paths between some pairs of barns (1 \leq N, M \leq 30001≤N,M≤3000). To shut the farm down, FJ plans to close one barn at a time. When a barn closes, all paths adjacent to that barn also close, and can no longer be used.
FJ is interested in knowing at each point in time (initially, and after each closing) whether his farm is "fully connected" -- meaning that it is possible to travel from any open barn to any other open barn along an appropriate series of paths. Since FJ's farm is initially in somewhat in a state of disrepair, it may not even start out fully connected.
FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱。
这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000)。为了关闭整个农场,FJ 计划每一次关闭掉一个谷仓。当一个谷仓被关闭了,所有的连接到这个谷仓的道路都会被关闭,而且再也不能够被使用。
FJ现在正感兴趣于知道在每一个时间(这里的“时间”指在每一次关闭谷仓之后的时间)时他的农场是否是“全连通的”——也就是说从任意的一个开着的谷仓开始,能够到达另外的一个谷仓。注意自从某一个时间之后,可能整个农场都开始不会是“全连通的”。
输入输出格式
输入格式:
The first line of input contains NN and MM. The next MM lines each describe a
path in terms of the pair of barns it connects (barns are conveniently numbered
1 \ldots N1…N). The final NN lines give a permutation of 1 \ldots N1…N
describing the order in which the barns will be closed.
输出格式:
The output consists of NN lines, each containing "YES" or "NO". The first line
indicates whether the initial farm is fully connected, and line i+1i+1 indicates
whether the farm is fully connected after the iith closing.
输入输出样例
4 3
1 2
2 3
3 4
3
4
1
2
YES
NO
YES
YES
分析:并查集的经典应用,无向图求联通用并查集,但是一个一个删点不好处理,那么就离线倒着加点,放到并查集里,看最后连通块的个数是不是只有1个,不过有一个坑点,加点后判断连通块个数时一定不能考虑没有加进去的点!程序里用flag数组标明了.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int maxn = ; int n,m,head[maxn],to[maxn * ],nextt[maxn * ],tot = ,fa[maxn],flag[maxn],a[maxn];
bool ans[maxn]; void add(int x,int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} int find(int x)
{
if (x == fa[x])
return x;
return fa[x] = find(fa[x]);
} int main()
{
scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
fa[i] = i;
for (int i = ; i <= m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
for (int i = n; i >= ; i--)
scanf("%d",&a[i]);
for (int i = ; i <= n; i++)
{
int cnt = ;
flag[a[i]] = ;
for (int j = head[a[i]];j;j = nextt[j])
{
int v = to[j];
if (flag[v])
{
int fx = find(a[i]),fy = find(v);
if (fx != fy)
fa[fx] = fy;
}
}
for (int j = ; j <= n; j++)
if (flag[j] && fa[j] == j)
cnt++;
if (cnt == )
ans[n + - i] = ;
else
ans[n + - i] = ;
}
for (int i = ; i <= n; i++)
{
if (ans[i])
printf("YES\n");
else
printf("NO\n");
} return ;
}
洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver的更多相关文章
- 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...
- 洛谷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 ...
随机推荐
- 微信小程序---scroll-view在苹果手机上触底或触顶页面闪动问题
在项目开发中遇到一个关于scroll-view的的问题,具体如下: 项目要求是横向滚动,由于直接在scroll-view组件设置display:flex不生效,因此考虑直接在scroll-view下增 ...
- 王者荣耀交流协会 — Alpha阶段中间产物
1. 版本控制 Coding :https://git.coding.net/SuperCodingChao/PSPDaily.git 2. 软件功能说明书 软件功能说明书发布在小组成员袁玥同学的博客 ...
- 感谢——Thunder团队
团队软件的开发,已经进入第二个阶段——Beta版本了.回头看看,我们走过了很长的一段路,也经历了很多,有意见不一的争吵.有取得暂时成功时的欢欣鼓舞,我们就像一家人,就像那首歌中唱到的,“我们是一家人, ...
- Thunder--Beta发布--美工+文案
作业:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1366 内容: 美工:原有功能展示.新增功能展示 程序图标 欢迎页面 我的书架 ...
- LAMP环境搭建Wordpress个人博客
LAMP简要介绍 L:LinuxA:Apache(httpd)M:MySQL , MariadbP:php, perl , python 静态资源:图片,文档,视频,HTML代码,CSS代码,js代码 ...
- haproxy入门 (作用: 高可用性,负载平衡和用于TCP和基于http的应用程序的代理)
安装haproxy 1:RPM包安装 yum install -y haproxy 2:编译安装 http://www.haproxy.org/#down 例如安装1.7.9版本 http:/ ...
- 使用TestNG 和 CSV文件进行数据驱动
package testNGPractice; import java.io.BufferedReader; import java.io.FileInputStream; import java.i ...
- VC的常用调试方法
前言 VS是非常强大的IDE,所以掌握VSVC的常用方法,将会使得我们找出问题解决问题事半功倍. 目录 VSVC的常用调试方法 前言 1. Watch窗口查看伪变量 2. 查看指针指向的一序列值 3. ...
- PHP简单模拟登录功能实例分享
1.curl实现模拟登录的代码,(只是实现服务器与服务器建立会话,其实并没有在客户端与服务器之间建立会话) <?php $cookie_jar = tempnam('./tmp','cookie ...
- wx import require的理解
服务器端的Node.js遵循CommonJS规范.核心思想是允许模块通过require 方法来同步加载所要依赖的其他模块,然后通过 exports或module.exports来导出需要暴露的接口. ...