一道并查集的(坑)题:关闭农场closing the farm(就是close不掉)

题目描述

in English:
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 \leq N, M \leq 30001≤N,M≤3000 ). 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.

in Chinese:
FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱。

这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000)。为了关闭整个农场,FJ 计划每一次关闭掉一个谷仓。当一个谷仓被关闭了,所有的连接到这个谷仓的道路都会被关闭,而且再也不能够被使用。

FJ现在正感兴趣于知道在每一个时间(这里的“时间”指在每一次关闭谷仓之前的时间)时他的农场是否是“全连通的”——也就是说从任意的一个开着的谷仓开始,能够到达另外的一个谷仓。注意自从某一个时间之后,可能整个农场都开始不会是“全连通的”。

说白了就是:

有N个农场,编号为1到N,有M条双向公路连接,其中1≤N,M≤200000,每次封锁一个农场,
当一个农场被封闭时,和该农场连接的道路全部断开,不能再通行。现在给出一个封锁顺序,
请计算每次封锁一个农场后,剩余的未封锁的农场是否连通。

输入输出格式

in English:

输入格式:
The first line of input contains NN and MM . The next MM lines each describe a

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

1 \ldots N1…N ). The final NN lines give a permutation of 1 \ldots N1…N

describing the order in which the barns will be closed.

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

indicates whether the initial farm is fully connected, and line i+1i+1 indicates

whether the farm is fully connected after the ii th closing.

in Chinese:

Input Data
第一行,包含两个整数N和M。
接下来M行,每行两个整数,描述一条道路。
接下来N行,每行一个整数,依次表示封锁的农场编号。

Output Data
输出包含N行,每行一个字符串“YES”或者“NO”,表示连通或者不连通。
其中第i行,表示第i个农场未被封锁时,所有未被封锁的农场的连通情况。
第i+1行,表示第i个农场被封锁后,所有未被封锁的农场的连通情况。
特别地,第1行表示所有农场未被封锁前,所有农场的连通情况。

输入输出样例

输入样例#1:
4 3
1 2
2 3
3 4
3
4
1
2
输出样例#1:
YES
NO
YES

然后我看着这道题就码出了第一个程序:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;
int n,m,f[210000];
bool close[210000],t[210000];
vector <int> pat[210000];

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

bool check()
{
    bool one=false;
    for(int i=1;i<=n;++i)
        f[i]=i;
    for(int i=1;i<=n;++i) if(!close[i])
    for(int j=0;j<pat[i].size();++j)
    {
        if(close[pat[i][j]]) continue;
        int fx=fa(i),fy=fa(pat[i][j]);
        if(fx!=fy) f[fx]=fy;
    }  
    /*然后这里判断连通图的个数的方法也很朴素, 就是查祖先的个数是否超过了1,就酱*/
    for(int i=1;i<=n;++i)
    if(!close[i] && f[i]==i)
    {
        if(!one) one=true;
        else return false;
    }
    return true;  //通过了检测之后直接返回TRUE(表示联通)
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i)  //就是记录了一下每个点的联通边
    {
        int from,to;
        scanf("%d%d",&from,&to);
        pat[from].push_back(to);
        pat[to].push_back(from);
    }

    if(check()) printf("YES\n"); //先是没删点的时候跑一遍check
    else printf("NO\n");

    for(int i=1;i<n;++i)//然后这里就是按顺序来,读一个点,删一个点,再看图是否联通
    {
        int x; scanf("%d",&x);
        close[x]=1;
        if(check()) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

然后我惊奇的发现洛谷上A了,这数据是真心水,这样也能水过去(吸口氧貌似还能再降三分之二的时间)。
AC!happy

然后看到这里哭了T_T
000
换个地儿评测TLE!还能不能愉快的玩耍了?

行,这数据真心强!我还真不信就倒在这儿了_ (:зゝ∠) _
于是乎我就换了个方法:做个倒过来的并查集。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;
int n,m,f[210000],add[210000];
bool open[210000],t[210000];
vector <int> pat[210000];

int read(){
    int x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) x=x*10+c-'0',c=getchar();
    return x;
}

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

bool check(int x)
{
    bool one=false;
    for(int i=0;i<pat[x].size();++i) 
    //没什么两样,就是把能和新开张的农场相连的农场合并了一下
    {
        if(!open[pat[x][i]]) continue;
        int fx=fa(x),fy=fa(pat[x][i]);
        if(fx!=fy) f[fx]=fy;
    }
    for(int i=1;i<=n;++i) 
     //判断还是老办法(TLE最根本的原因也是在这里,这个等会儿讲)
        if(open[i] && f[i]==i)
        {
            if(!one) one=true;
            else return false;
        }
    return true;
}

int main()
{
    n=read();m=read();
    for(int i=1;i<=m;++i)
    {
        int from=read(),to=read();
        pat[from].push_back(to);
        pat[to].push_back(from);
    }
    for(int i=1;i<=n;++i)  //并查集的预处理就直接放在主函数里了
        f[i]=i;

    for(int i=1;i<=n;++i)  //然后实现倒着把数据读入
        add[n-i]=read();
    open[add[0]]=1;  //表示第一家农场开门
    t[n]=1;
    for(int i=1;i<n;++i)
    {
        open[add[i]]=1; //然后一家接着一家开门(抢生意)
        if(check(add[i]))  t[n-i]=1;  //如果check出来图联通了,就(反着)标记t为1
    }
    for(int i=1;i<=n;++i)  //然后按读入的顺序输出
    if(t[i]) printf("YES\n");
    else printf("NO\n");

    return 0;
}

然后洛谷上一交,心里美滋滋:
Orz

时间降了十倍左右!这下还会爆就真是。。。额

666

兵败如山倒= =
0

_ ( :зゝ∠)_这次是真爬不起来了QAQ

然而我最后还是爬起来了_ ( :зゝ∠)_ (其实也就是做了个判断优化 = =|||)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;
int n,m,ans,f[210000],add[210000];
bool open[210000],t[210000];
vector <int> pat[210000];

int read(){
    int x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) x=x*10+c-'0',c=getchar();
    return x;
}

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

bool check(int x)
{
    ++ans; //其他我都不想介绍了,主要也就是这里。。。
    //ans++代表多了一个图(新开张的农场自为一个图)
    for(int i=0;i<pat[x].size();++i)
    {
        if(!open[pat[x][i]]) continue;
        int fx=fa(x),fy=fa(pat[x][i]);
        if(fx!=fy) f[fx]=fy,ans--;
        //然后在这里进行一个减图操作,如果两个集合合并了,图必然--,然后ans就--了
    }

    if(ans) //最后判断一下多余的图是否为0,是的话就返回FALSE就这么easy(卡了我60)
        return false;
    return true;
}

int main()
{
    n=read();m=read();
    for(int i=1;i<=m;++i)
    {
        int from=read(),to=read();
        pat[from].push_back(to);
        pat[to].push_back(from);
    }
    for(int i=1;i<=n;++i)
        f[i]=i;

    for(int i=1;i<=n;++i)
        add[n-i]=read();
    open[add[0]]=1;
    for(int i=1;i<n;++i)
    {
        open[add[i]]=1;
        if(check(add[i]))  t[n-i]=1;
    }
    for(int i=1;i<n;++i)
        if(t[i]) printf("YES\n");
        else printf("NO\n");
    printf("YES\n");

    return 0;
}

然后洛谷上又A了一遍,时间又降了一半的样子(虽说A了还是没有什么喜悦的赶脚,毕竟连跪了两把T_T)
120ms

然后又去挑战了一下BOSS:

A了!

终于A了!_ (||Xゝ∠)_
喜极而泣

然后我就这样跪着(不如说是躺着)过了一道(巨)坑。。。 _ (:зゝ∠)_

然后本blog就结束了,童鞋们下次见!

猜你喜欢

转载自blog.csdn.net/qq_35416331/article/details/79767194