Codeforces 1131F Asya And Kittens 并查集

Posted 00isok

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1131F Asya And Kittens 并查集相关的知识,希望对你有一定的参考价值。

<题目链接>

题目大意:
有$1~n$只小猫,开始将它们放在指定的n个单元格内,然后随机从n-1个隔板中拆除隔板,最终使得这些小猫在同一单元格。现在依次给出拆除隔板的顺序,比如:1 4 就表示1号和4号小猫之间的隔板会被拆除(注:只能拆除相邻区域小猫之间的隔板)。

解题分析:
利用并查集处理,$nxt[i]$ 表示 $i$ 之后的小猫序号, $mxri[i]$表示以第$i$个小猫已确定区域的最右端(方便其它小猫接入)。

#include <bits/stdc++.h>
using namespace std;

#define N int(2e5+7)
#define rep(i,s,t) for(int i=s;i<=t;i++)
int n;
int nxt[N],fa[N],mxri[N];
int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); }
int main(){
    scanf("%d",&n);
    rep(i,1,n)fa[i]=i,mxri[i]=i;
    rep(i,1,n-1){
        int x,y;scanf("%d%d",&x,&y);
        x=find(x),y=find(y);
        nxt[mxri[x]]=y;   //将y接在x最右边的元素右边
        mxri[x]=mxri[y];    //将y所连着的元素一块接上
        fa[y]=x;
    }
    for(int i=find(1);i;i=nxt[i])printf("%d ",i);
}

 

 

2019-02-24

以上是关于Codeforces 1131F Asya And Kittens 并查集的主要内容,如果未能解决你的问题,请参考以下文章

CF1131F Asya And Kittens并查集,暴力,新颖建图思维

CodeForces 621AWet Shark and Odd and Even

codeforces 621A Wet Shark and Odd and Even

CodeForces 621A Wet Shark and Odd and Even

codeforces 621D Rat Kwesh and Cheese

codeforces 621C Wet Shark and Flowers