zoukankan      html  css  js  c++  java
  • 并查集

    在这里插入图片描述

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<queue>
    #include<vector>
    
    using namespace std;
    #define maxn 200005
    
    int a1,a2,a3,f[maxn],n,m;
    int getf(int o){                //找祖宗 
        if (f[o]==o)return o;
        else return f[o]=getf(f[o]);//路径压缩
    }
    void merge(int v,int u){        //合并子集 
        int t1,t2;
        t1=getf(v);
        t2=getf(u);
        if(t1!=t2)f[t2]=t1;
        return;
    }
    void find(int v,int u) {
        int t1,t2;
        t1=getf(v);
        t2=getf(u);
        if (t1==t2)printf("Y
    ");
        else printf("N
    ");
    }
    int main(){
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)
            f[i]=i;
        for(int i=1;i<=m;i++){
            scanf("%d %d %d",&a1,&a2,&a3);
            if(a1==1) merge(a2,a3);
            else find(a2,a3);
        }
        return 0;
    }
    希望用自己的努力为自己赢得荣誉。
  • 相关阅读:
    hdu 4370
    lightoj 1074
    poj 1026
    poj 3159
    poj3660 cow contest
    hdu 4069 垃圾数独
    操作系统概念题复习
    ARM指令
    C++ 抢占时优先级进程调度
    Docker 入门
  • 原文地址:https://www.cnblogs.com/Mmasker/p/11917489.html
Copyright © 2011-2022 走看看