[Template] 2 disjoint-set

 

 

 

#include<iostream>
#include<cstdio>
using namespace std;
int fa[10001],z,n,m,x,y;
int getfather(int w)
{
    if(fa[w]==w)
    return w;
    fa[w]=getfather(fa[w]);
    return fa[w];
}
void together(int a,int b)
{
    fa[getfather(a)]=getfather(b);
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
    }
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y>>z;
        if(x==1)
        {
            together(y,z);
        }
        if(x==2)
        {
            int yy=getfather(y);
            int zz=getfather(z);
            if(yy==zz)
            cout<<"Y"<<endl;
            else 
            cout<<"N"<<endl;
        }
    }
    return 0;
}                                                                                                                                                     

                               

                                                   2019-09-08 By the fourteenth day of summer

 


 

Guess you like

Origin www.cnblogs.com/north-star/p/11489287.html