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

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;

const int N = 200050;

int n,m,cs;
int b[N],p[N],f[N],ans[N];
vector< int > g[N];

inline int in(int x=0){ scanf("%d",&x);return x; }
int find(int x){ return f[x] == x ? x : f[x]=find(f[x]); }
int main(){
    n=in(),m=in();
    for(int i=1,u,v;i<=m;i++){
        u=in(),v=in();
        g[u].push_back(v),g[v].push_back(u);
    }
    for(int i=1;i<=n;i++) p[i]=in();
    for(int i=1;i<=n;i++) f[i]=i;

    for(int x=n,u,v;x;--x){
        cs++;
        u=p[x],b[u]=1;
        for(int i=0,lim=g[u].size();i<lim;i++){
            v=g[u][i];
            if(b[v]) if(find(u) != find(v)) f[find(u)]=find(v),cs--;
        }
        if(cs > 1) ans[x]=0;else ans[x]=1;
    }
    for(int i=1;i<=n;i++) if(ans[i]) puts("YES");else puts("NO");
    return 0;
}

  

时间: 2024-10-12 04:13:39

BZOJ 4579: [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

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

BZOJ 4576: [Usaco2016 Open]262144

Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco2016 Open]248 加强版. 做248的那个区间DP其实很多方案都是0,而且一个区间中只有一个合法的数字. 然后就是 一个区间合成一个数的方案的这个数字是固定的. \(f[i][j]\) 表示以 \(i\) 结尾是否能合成 \(j\),同时记录一下转移位置,每次向前找前一个指针就可以了. 复

[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

BZOJ 4742: [Usaco2016 Dec]Team Building

4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 16[Submit][Status][Discuss] Description Every year, Farmer John brings his NN cows to compete for "best in show" at the state fair. His arch -rival, F

bzoj 4412: [Usaco2016 Feb]Circular Barn

4412: [Usaco2016 Feb]Circular Barn Description 有一个N个点的环,相邻两个点距离是1.点顺时针标号为1..N.每一个点有ci头牛,保证∑ci=N.每头牛都可以顺时针走.设一头牛走了d个单位停下了,将耗费d^2的能量.请设计一种牛的走法,使得每一个点上都正好有一头牛,且最小化耗费的能量. Input 第一行一个数N.N <= 100000接下来N行,每行一个数ci. Output 输出一个数表示耗费能量的最小值 Sample Input 10 1 0

bzoj 4506: [Usaco2016 Jan]Fort Moo

4506: [Usaco2016 Jan]Fort Moo Description Bessie is building a fort with her friend Elsie. Like any good fort, this one needs to start with a sturdy frame. Bessie wants to build a frame in the shape of a one-meter-wide rectangular outline, atop which

Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解

4582: [Usaco2016 Open]Diamond Collector Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: 136[Submit][Status][Discuss] Description Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! Sh