• 【模板】并查集


    只是再自己打一遍熟悉一些

    #include<iostream>
    #include<cstdio>
    using namespace std;
    #define tcl(a,b,c) for(a=b;a<=c;a++)
    int f[100001];
    int get(int x)
    {
        if(f[x]==x)
             return x;
        else 
        {
            f[x]=get(f[x]);
            return f[x];
        }
    }
    void join(int a,int b)
    {
        int t1,t2;
        t1=get(a);
        t2=get(b);//这里不能直接赋值不知道为什么..
        if(t1!=t2)
            f[t2]=t1;
        return;
    }
    void find(int a,int b)
    {
        int t1,t2;
        t1=get(a);
        t2=get(b);
        if(t1==t2) 
        {
            printf("Y
    ");return;
        }
        printf("N
    ");
    }
    int main()
    {
        int n,m,i,t,a,b;
        scanf("%d%d",&n,&m);
        tcl(i,1,n) f[i]=i;
        tcl(i,1,m)
        {
            scanf("%d%d%d",&t,&a,&b);
            if(t==1) 
            {
                join(a,b);
            }
            else
            {
                find(a,b);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Flink
    数据工程师
    数据库中间件
    数据仓库
    数据库
    设计模式
    机器学习
    Collections
    Concurrency
    Java
  • 原文地址:https://www.cnblogs.com/LSWorld/p/bcj1.html
Copyright © 2020-2023  润新知