洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
题目描述
FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱。
这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000)。为了关闭整个农场,FJ 计划每一次关闭掉一个谷仓。当一个谷仓被关闭了,所有的连接到这个谷仓的道路都会被关闭,而且再也不能够被使用。
FJ现在正感兴趣于知道在每一个时间(这里的“时间”指在每一次关闭谷仓之后的时间)时他的农场是否是“全连通的”——也就是说从任意的一个开着的谷仓开始,能够到达另外的一个谷仓。注意自从某一个时间之后,可能整个农场都开始不会是“全连通的”。
输入输出格式
输入格式:
The first line of input contains $N$ and $M$. The next $M$ lines each describe a
path in terms of the pair of barns it connects (barns are conveniently numbered
$1 \ldots N$). The final $N$ lines give a permutation of $1 \ldots N$
describing the order in which the barns will be closed.
输出格式:
The output consists of $N$ lines, each containing "YES" or "NO". The first line
indicates whether the initial farm is fully connected, and line $i+1$ indicates
whether the farm is fully connected after the $i$th closing.
输入输出样例
4 3 1 2 2 3 3 4 3 4 1 2
YES NO YES YES
代码
题目大意:每次删除一个点询问剩下的点是否连通。
逆序并查集维护。
#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int can[3001],n; int fa[3001]; int ans[3001],a[3001]; int h[3001],ne[6001],to[6001],fr[6001],en=0; inline void add(int a,int b) {ne[en]=h[a];to[en]=b;fr[en]=a;h[a]=en++;} inline int gf(int k) {if (fa[k]==k) return k;else return fa[k]=gf(fa[k]); } int main() {memset(h,-1,sizeof h);int n,m;scanf("%d%d",&n,&m);for (int i=1;i<=m;++i){int a,b;scanf("%d%d",&a,&b);add(a,b);add(b,a);}for (int i=1;i<=n;++i) scanf("%d",&a[i]),fa[i]=i;for (int i=n;i>=1;--i){can[a[i]]=1;for (int j=h[a[i]];j>=0;j=ne[j]){if (can[fr[j]]&&can[to[j]]){int l=fr[j],r=to[j];int fl=gf(l),fr=gf(r);if (fl!=fr) fa[fl]=fr;}}int cnt=0;for (int j=1;j<=n;++j)if (can[j]&&fa[j]==j) cnt++;if (cnt==1) ans[i]=1;}printf("YES\n");for (int i=2;i<=n;++i) if (ans[i]) printf("YES\n");else printf("NO\n"); }
转载于:https://www.cnblogs.com/huihao/p/7788550.html
洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver相关推荐
- 一道并查集的(坑)题:关闭农场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通过指定奶牛必须遵 ...
- 洛谷 P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- 洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm
题目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety ...
- 洛谷 P2921 在农场万圣节Trick or Treat on the Farm (tarjan求强连通分量)
洛谷 P2921 在农场万圣节Trick or Treat on the Farm (tarjan求强连通分量) 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N&l ...
- 洛谷 P1550 浇水
洛谷 1550 浇水 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= ...
- 洛谷 P2296 寻找道路
感慨 周五比赛的测试题,结果到比赛结束也没有读懂题意...给的样例太少了,我一直以为我是不是spfa敲错了...没想到中间还有卡的地方 分析 题目中的一句耐人寻味的话"路径上的所有点的出边所 ...
- 洛谷 P3184 [USACO16DEC]Counting Haybales数草垛
洛谷 P3184 [USACO16DEC]Counting Haybales数草垛 题目描述 Farmer John has just arranged his NN haybales (1 \leq ...
- 广度优先搜索——USACO08FEB(洛谷 P2895)
本文要讲述的是一道既简单又复杂的一道题,值得我们好好去思考并求解. 题目出自洛谷P2895 同时也是USACO2008年的一道题. 其中主要问题是如何将问题转化为对应的数组或图?如何判断满足的条件情况 ...
最新文章
- python list的extend (会将被插入的列表的每个元素从列表中拿出添加到列表中)与append方法(若被插入为列表,会将列表插入到源列表中)区别
- 这 3 个字是未来发展关键,不重视的企业,正在被淘汰
- mysql数据库技术实验小结_Mysql数据库事务实验以及总结
- C#/SQL 上周本周
- mysql 取消密码警告
- MFC 简单输出EXCEL - (OLE)
- 导向滤波实现代码以及使用颜色先验去雾算法
- 国企“造船”转行测试,成功拿下11K,如今谁又甘心平庸呢?
- Linux Capability探索实验
- 穷人和富人的距离0.05厘米
- 在Excel数据最后一行下面写入数据的一种方法
- iOS 玩转微信——通讯录
- 【洛谷1144】最短路计数 最短路
- 状压搜索 Circling Round Treasures:CodeForces - 375C
- Java多线程编程——线程同步与线程安全问题及synchronized关键字
- 你不能不了解的《3P通道+3P功能》
- pygame-1.9.6-cp38-cp38-win_amd64.whl百度云下载
- vip混合测试v号打卡好的卡仕达看哈看收到货卡仕达库哈斯
- 【EDA365电子论坛】RISC-V 能否超越 x86、Arm,成为新一代计算机系统架构?
- Bugtags 移动时代bug管理系统利器
热门文章
- iis php5.2 cgi,在PHP 5.2.6 / IIS CGI中清空$_POST数组
- laravel mysql like_3分钟短文|Laravel 使用like匹配字符串的用法示例
- OLTP和OLAP是什么
- 7 ida pro 网盘_IDA分析iOS网络协议
- Mozilla 的 Flash 杀手 'Shumway' 已经现身
- 十四、w、vmstat、top、sar命令
- CocoaPods集成ShareSDK
- Failure [INSTALL_FAILED_OLDER_SDK] [每件问题100块]
- word2010生成目录的方法
- .NET中使用Memcached的相关资源整理