题目描述

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.

输入输出样例

输入样例#1:

4 3
1 2
2 3
3 4
3
4
1
2
输出样例#1:

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的更多相关文章

  1. 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

    传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...

  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. mybatis 加载配置文件的方法

    一.  使用sqlSessionFactory 的 mapperLocations 进行加载 <!-- SessionFactory --> <bean id="sqlSe ...

  2. Dev C++支持ISOC++11标准

    报错如下: [Error] #error This file requires compiler and library support for the ISO C++ 2011 standard. ...

  3. 5个最优秀的微信小程序UI组件库

    开发微信小程序的过程中,选择一款好用的组件库,可以达到事半功倍的效果.自从微信小程序面世以来,不断有一些开源组件库出来,下面5款就是排名比较靠前,用户使用量与关注度比较高的小程序UI组件库.还没用到它 ...

  4. Fiber Network ZOJ 1967(Floyd+二进制状态压缩)

    Description Several startup companies have decided to build a better Internet, called the "Fibe ...

  5. Spring演示及总结

    一.目标 二.分工 三.回顾 发现问题: 第一个冲刺的任务以基本完成,但队友的状态相对有些疲软,主要原因可能是这两周有好几个课程大作业要赶, 有的队友还要为比赛做准备,及做一些其他是项目,时间较紧,有 ...

  6. Scrum立会报告+燃尽图(Beta阶段第二周第四次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2412 项目地址:https://coding.net/u/wuyy694 ...

  7. resx文件引用

    应用场景: 自己在编写双语界面的时候,用到两种语言表. 引用如下: LocRM = new ResourceManager("TMI_E.words_" + lang.ToLowe ...

  8. 2018软工实践—Alpha冲刺(6)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 测试服务器并行能力 学习MSI.CUDA ...

  9. BETA阶段冲刺集合

    冲刺开始: https://www.cnblogs.com/LZTZ/p/9097296.html 第一天: https://www.cnblogs.com/LZTZ/p/9097303.html 第 ...

  10. dubbo源码分析1——负载均衡

    dubbo中涉及到的负载均衡算法只要有四种:Random LoadBalance(随机均衡算法).RoundRobin LoadBalance(权重轮循均衡算法).LeastAction LoadBa ...