码迷,mamicode.com
首页 > 其他好文 > 详细

【模板】2 并查集

时间:2019-09-08 23:59:20      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:std   col   turn   div   get   ==   mamicode   main   void   

技术图片

 

 技术图片

 

#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十四日的夏

 

【模板】2 并查集

标签:std   col   turn   div   get   ==   mamicode   main   void   

原文地址:https://www.cnblogs.com/north-star/p/11489287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!