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 NNN barns connected with MMM bidirectional paths between some pairs of barns (1≤N,M≤30001 \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 NNN and MMM. The next MMM lines each describe a

path in terms of the pair of barns it connects (barns are conveniently numbered

1…N1 \ldots N1…N). The final NNN lines give a permutation of 1…N1 \ldots N1…N

describing the order in which the barns will be closed.

输出格式:

The output consists of NNN lines, each containing "YES" or "NO". The first line

indicates whether the initial farm is fully connected, and line i+1i+1i+1 indicates

whether the farm is fully connected after the iiith closing.

输入输出样例

输入样例#1:
复制

4 3
1 2
2 3
3 4
3
4
1
2

输出样例#1: 复制

YES
NO
YES
YES




1.解法一 n**2暴力2.并查集

,如果点数足够多,图足够稠密,那么这么多遍spfa等着T就可以了,实际上这个题直接bfs暴力遍历整张图就可以了,当然是对于这个题的数据而言。测试的时候的数据,貌似200000,导致bfs也会T飞6个点,所以这个题最好的解法貌似是并查集。然而这个题一会关闭这个点一会关闭那个点看着好难受。。那么就先读入所有点,然后反向循环,一个一个往里加,记录下来,最后再顺序输出就可以了,楼下貌似也已经有过并查集了,只是可能不是特别严谨(毕竟这个题完全用不着这么做,直接暴力打起来更舒服)。我就挂上200000版本的吧。。

看到删除一个点,就像是删除一个集合一样,应该想到并查集。code:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int n,m,p[200010],a[400010],nxt[400010],father[400010],now,q[200010],tot;
 6 bool ans[200010],ext[200010];
 7 void add(int x,int y){
 8     tot++;a[tot]=y;nxt[tot]=p[x];p[x]=tot;
 9 }
10 int find(int x){
11     if (father[x]==x) return x;
12     father[x]=find(father[x]);
13     return father[x];
14 }
15 int main()
16 {
17 //    freopen("closing.in","r",stdin);
18 //    freopen("closing.out","w",stdout);
19     scanf("%d%d",&n,&m);
20     for (int i=1;i<=m;i++){
21         int x,y;scanf("%d%d",&x,&y);
22         add(x,y);add(y,x);
23     }
24     for (int i=1;i<=n;i++){
25         scanf("%d",&q[i]);father[i]=i;
26     }
27     now=0;
28     for (int i=n;i>=1;i--){
29         ++now;ext[q[i]]=true;
30         for (int j=p[q[i]];j!=0;j=nxt[j])
31           if (ext[a[j]]==true){
32                 int r1,r2;
33                 r1=find(q[i]);r2=find(a[j]);
34                 if (r1!=r2){
35                      --now;father[r1]=r2;
36               }
37           }
38         if (now==1) ans[i]=true;
39     }
40     for (int i=1;i<=n;i++)
41       if (ans[i]==true) printf("YES\n");
42       else printf("NO\n");
43     return 0;
44 }


原文地址:https://www.cnblogs.com/zhangbuang/p/10290444.html

时间: 2024-10-09 21:40:08

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 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

luogu 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

洛谷 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; Edg

洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm

题目描述 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  barns connected with  bidirectional paths between some pairs of barn

[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 NNN barns connected with MMM bidirectional paths between some pairs o

[USACO16OPEN]关闭农场Closing the Farm(洛谷 3144)

题目描述 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  barns connected with  bidirectional paths between some pairs of barn

P3144 关闭农场 并查集 反向

FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱. 这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000).为了关闭整个农场,FJ 计划每一次关闭掉一个谷仓.当一个谷仓被关闭了,所有的连接到这个谷仓的道路都会被关闭,而且再也不能够被使用. FJ现在正感兴趣于知道在每一个时间(这里的“时间”指在每一次关闭谷仓之前的时间)时他的农场是否是“全连通的”——也就是说从任意的一个开着的谷仓开始,能够到达另外的一个谷仓.注意自从某一个时间之后,可

[无向图判连通]4.17平平凡凡才是真

因为下雨外加睡不醒所以这几天状态萎靡不振 决定开始刷题了,保持每天一道或者三天两道的进度吧 知识点随着刷题慢慢补吧 放弃爆炸oj了,蒟蒻的自我救赎 今天的题是[USACO16OPEN]关闭农场Closing the Farm 本蒟蒻的第一反应:强行spfa判连通 事实证明如果不是不知道哪里写错的玄学错误应该是能拿50分的 看了一下题解 思路大概是不考虑删点 从最后一个点开始加点判断连通与否 然后再倒着输出就好 要求:离线操作 具体实施:并查集 我和你连通,爸爸就一样 一旦出现两个或两个以上爸爸,

4579: [Usaco2016 Open]Closing the Farm

4579: [Usaco2016 Open]Closing the Farm Description 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