洛谷 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;
    Edge(int x=0,int y=0,int nxt=0):
        x(x),y(y),nxt(nxt){}
}edge[N<<1];

void add(int x,int y){
    edge[++sumedge]=Edge(x,y,head[x]);
    head[x]=sumedge;
}

int f(int x){
    return fa[x]==x?x:fa[x]=f(fa[x]);
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);add(y,x);
    }
    for(int i=1;i<=n;i++)fa[i]=i;cnt=n;
    for(int i=1;i<=n;i++)scanf("%d",&q[i]);
    for(int i=n;i>=1;i--){
        int x=q[i];exit[x]=true;
        for(int h=head[x];h;h=edge[h].nxt){
            int v=edge[h].y;
            if(exit[v]==0)continue;
            int fx=f(x),fy=f(v);
            if(fx!=fy){
                fa[fx]=fy;
                cnt--;
            }
        }
        if(cnt==i)ans[i]=true;
    }
    for(int i=1;i<=n;i++)
     if(ans[i])puts("YES");
     else puts("NO");
    return 0;
}

并查集

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 3009
using namespace std;

int n,m,cnt;

int q[N],exit[N],ans[N],d[N][N];

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        d[x][y]=d[y][x]=true;
    }
    for(int i=1;i<=n;i++)scanf("%d",&q[i]);
    for(int i=n;i>=1;i--){
        bool flag=false;
        int now=q[i];
        exit[++cnt]=now;
        for(int k=1;k<=cnt;k++){
            for(int j=1;j<=cnt;j++){
                for(int p=1;p<=cnt;p++){
                    d[exit[j]][exit[p]]=d[exit[j]][exit[p]]||(d[exit[j]][exit[k]]&&d[exit[k]][exit[p]]);
                }
            }
        }
        for(int j=1;j<=cnt;j++){
            for(int p=j+1;p<=cnt;p++){
                if(d[exit[j]][exit[p]]==0){
                    ans[i]=0;flag=true;
                    break;
                }
            }
        }
        if(flag==false){
            ans[i]=1;
        }
    }
    for(int i=1;i<=n;i++)
     if(ans[i])printf("YES\n");
     else printf("NO\n");
    return 0;
}

50暴力

时间: 2024-11-08 12:39:27

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

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

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(并查集反向)

题目描述 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_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

洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector

题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected  diamonds () of varying sizes, and she wants to arrange some of them in a pair of display cases in the barn. Since Bessie

洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector

题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected NN diamonds (N \leq 50,000N≤50,000) of varying sizes, and she wants to arrange some of them in a pair of display cases in

洛谷 P3146 [USACO16OPEN]248

P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small touch screen rather cumbersome to use with her large hooves. She is particularly intrigued by the current game she is playing.The