【Luogu3367】【模板】并查集

problem

solution

codes

#include<iostream>
using namespace std;
int fa[200010];
int find(int x){return x==fa[x] ? x : fa[x]=find(fa[x]);}
int main(){
    int n, m;
    cin>>n>>m;
    for(int i = 1; i <= n; i++)fa[i] = i;
    for(int i = 1; i <= m; i++){
        int a, b, c;  cin>>a>>b>>c;
        if(a == 1)fa[find(b)]=find(c);
        else cout<< (find(b)==find(c)?"Y":"N")<<"\n";
    }
    return 0;
}

posted @ 2018-06-08 21:58  gwj1139177410  阅读(80)  评论(0编辑  收藏  举报
选择