bzoj4579: [Usaco2016 Open]Closing the Farm

bzoj4579: [Usaco2016 Open]Closing the Farm

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 24  Solved: 17
[Submit][Status][Discuss]

Description

Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporar

ily close down his farm to save money in the meantime.The farm consists of NN barns connected with M

M bidirectional paths between some pairs of barns (1≤N,M≤200,000). 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 ca

n no longer be used.FJ is interested in knowing at each point in time (initially, and after each clo

sing) whether his farm is "fully connected" -- meaning that it is possible to travel from any open b

arn to any other open barn along an appropriate series of paths. Since FJ‘s farm is initially in som

ewhat in a state of disrepair, it may not even start out fully connected.

Input

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…N). The final N lines give a permutation o

f 1…N describing the order in which the barns will be closed.

Output

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 th

e iith closing.

Sample Input

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

Sample Output

YES
NO
YES
YES

HINT

Source

直接在线的话lct似乎是可以做的?

然而可以离线。。。所以就变成傻逼题了。。。

离线后倒过来加边 并查集维护一下联通块个数即可

#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define N 500000
using namespace std;
struct E{
    int to,next;
}e[N];
int n,m,out[N],f[N],tot,s[N],flag[N],head[N];
inline int read() {
     register int x=0,ch=getchar();
     while(ch<‘0‘||ch>‘9‘) ch=getchar();
     while(ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-48,ch=getchar();
     return x;
}
int find(int x) {
     if(f[x]==x) return x;else return f[x]=find(f[x]);
}
inline void ins(int u,int v) {
     e[++tot].to=v; e[tot].next=head[u]; head[u]=tot;
}
int main () {
     register int a,b,cnt,x,y;
     scanf("%d%d",&n,&m);
     rep(i,1,m) {a=read(); b=read(); ins(a,b); ins(b,a);}
     rep(i,1,n) s[i]=read(),flag[s[i]]=1,f[i]=i; cnt=n;
     rep(i,1,n) if(!flag[i]){
          for(register int k=head[i];k;k=e[k].next) {
               if(!flag[e[k].to]) {
                    x=find(i); y=find(e[k].to);
                    if(x!=y) f[x]=y,--cnt;
               }
          }
     }
     for(register int i=n;i;i--) {
          flag[s[i]]=0;
          for(register int k=head[s[i]];k;k=e[k].next) if(!flag[e[k].to]) {
               x=find(s[i]); y=find(e[k].to);
               if(x!=y) f[x]=y,--cnt;
          }
          out[i]=(cnt==i);
     }
     rep(i,1,n) if(!out[i]) puts("NO");else puts("YES");
}

时间: 2024-11-12 02:23:15

bzoj4579: [Usaco2016 Open]Closing the Farm的相关文章

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

BZOJ 4579: [Usaco2016 Open]Closing the Farm

Description 依次删去一个点和它的边,问当前图是否连通. Sol 并查集. 倒着做就可以了. 每次将一个点及其的边加入,如果当前集合个数大于 1,那么就不连通. Code /************************************************************** Problem: 4579 User: BeiYu Language: C++ Result: Accepted Time:2196 ms Memory:10328 kb *********

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

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

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

浴谷夏令营题单

这是群里一位神犇整理的,我只负责将它们做完. 一.暴力.搜索Luogu 1588 丢失的牛Luogu 1463 [SDOI2005]反素数antBzoj 1085 [SCOI2005]骑士精神Luogu 1019 单词接龙Luogu 1078 文化之旅Luogu 1312 Mayan游戏Luogu 3823 蚯蚓排队Codeforces 444B Codeforces 555DLuogu 1979 华容道 二.初等数论Poj 3292 H合成数Luogu 1890 gcd区间Luogu 1029

bzoj4745: [Usaco2016 Dec]Cow Checklist

bzoj4745: [Usaco2016 Dec]Cow Checklist Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1  Solved: 1[Submit][Status][Discuss] Description Every day, Farmer John walks through his pasture to check on the well-being of each of his cows. Onh is farm he h

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

bzoj4510: [Usaco2016 Jan]Radio Contact

bzoj4510: [Usaco2016 Jan]Radio Contact Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 17[Submit][Status][Discuss] Description Farmer John has lost his favorite cow bell, and Bessie the cow has agreed to help him find it! They both fan ou