Disjoint Set: Union Find Tree Aizu - DSL_1_A

版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/84933420
#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAX = 10005;

int pre[MAX];

int Find(int x){
    return (pre[x] == x) ? x : Find(pre[x]);
}

void init()
{
    for(int i=0; i<MAX; i++)
        pre[i]=i;
    return;
}

int main ()
{
    init();

    int n, m;

    cin >> n >> m;

    int com, x, y;

    while(m--){
        cin >> com >> x >> y;

        x = Find(x);
        y = Find(y);

        if(com == 0)
            pre[x] = y;//合并
        else if(x == y)
            cout << 1 << endl;
        else cout << 0 << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/KLFTESPACE/article/details/84933420