码迷,mamicode.com
首页 > 其他好文 > 详细

P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver(并查集反向)

时间:2019-01-19 11:27:42      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:NPU   ext   rect   min   close   possible   with   algorithm   start   




 

题目描述

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 30001N,M3000). 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 N1N). The final NNN lines give a permutation of 1…N1 \ldots N1N

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 }

 















P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver(并查集反向)

标签:NPU   ext   rect   min   close   possible   with   algorithm   start   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!