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

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

题解:

首先这道题目什么意思呢???

大概是讲用m条边连接的农场,每次关闭一个谷仓,问你每次在关闭谷仓之前整个农场是否完全联通。

知道意思后这就是到很裸的并查集了,维护联通块个数,看看是不是1就行了。。。

#include<stdio.h>
#include<iostream>
using namespace std;
const int N=200005;
int n,m,i,j,x,y,fx,fy,cnt,a[N],p[N],ans[N],f[N];
int tot,head[N],Next[N<<1],to[N<<1];
void add(int x,int y)
{
    tot++;
    to[tot]=y;
    Next[tot]=head[x];
    head[x]=tot;
}
int get(int x)
{
    if(f[x]==x) return x;else return f[x]=get(f[x]);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
        head[i]=-1;
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    for(i=1;i<=n;i++)
        f[i]=i;
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
    cnt=0;
    for(i=n;i>=1;i--)
    {
        cnt++;
        x=a[i];
        for(j=head[x];j!=-1;j=Next[j])
        {
            y=to[j];
            if(p[y]==1)
            {
                fx=get(x);
                fy=get(y);
                if(fx!=fy)
                {
                    cnt--;
                    f[fx]=fy;
                }
            }
        }
        ans[i]=cnt;
        p[x]=1;
    }
    for(i=1;i<=n;i++)
        if(ans[i]==1) printf("YES\n");else printf("NO\n");
    return 0;
}
时间: 2024-07-30 04:31:58

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

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

bzoj4579: [Usaco2016 Open]Closing the Farm

bzoj4579: [Usaco2016 Open]Closing the Farm Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 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 cl

[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