【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 barns (). 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 and . The next lines each describe a

path in terms of the pair of barns it connects (barns are conveniently numbered

). The final lines give a permutation of

describing the order in which the barns will be closed.

輸出格式:
The output consists of lines, each containing “YES” or “NO”. The first line

indicates whether the initial farm is fully connected, and line indicates

whether the farm is fully connected after the th closing.

輸入輸出樣例

輸入樣例#1:
4 3
1 2
2 3
3 4
3
4
1
2
輸出樣例#1:
YES
NO
YES
YES

題解:如果判斷每刪除一個點的時候,圖是否聯通,也可以反着來思考。從最後開始循環,每當加入一個點的時候,能否保證已經加入的點是聯通的,那麼便可以用並查集來實現。每當加入一個點,將與該點聯通的點加入並查集,看已經在圖裏的點是否和該並查集相連,便可得出答案。

代碼:

#include<iostream>
#include<cstdio>
#define N 200006
using namespace std;
struct node
{
    int from,to,next;
}e[N*2];
int n,m;
int e_num,head[N],a[N];
bool flag[N],ans[N];
int father[N];
int get()
{
    int x=0,f=1;
    char c;
    c=getchar();
    if (c==' ') return x*f;
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}
int find(int x)
{
    if (father[x]!=x) father[x]=find(father[x]);
    return father[x];
}
void mix(int x,int y)
{
    int xx,yy;
    xx=find(x);
    yy=find(y);
    father[yy]=xx;
}
void add(int from,int to)
{
    ++e_num;
    e[e_num].from=from;
    e[e_num].to=to;
    e[e_num].next=head[from];
    head[from]=e_num;
}
int main()
{
    int x,y,i,j,tmp;
    n=get();m=get();
    for (i=1;i<=m;i++)
    {
        x=get();y=get();
        add(x,y);
        add(y,x);
    }
    for (i=n;i>=1;i--)
    {
        a[i]=get();
        father[i]=i;
    }   
    int sum=1;
    for (i=1;i<=n;i++)
    {
        x=a[i];
        flag[x]=true;
        for (j=head[x];j;j=e[j].next)
        {
            if (find(e[j].to)!=find(x)&&flag[e[j].to])
            {
                mix(e[j].to,x);
                sum++;
            }
        }
        if (sum==i) ans[i]=1;
        else ans[i]=0;
    }
    for (i=n;i>=1;i--)
    {
        if (ans[i]==1) printf("YES\n");
        else printf("NO\n");
    }
} 
發佈了79 篇原創文章 · 獲贊 32 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章