Template. Disjoint set

2018-08-07

Disjoint set並查集。

Code

#define maxn 1000
struct Disjoint {
    int p[maxn];
    void init() {
        for (int i = 0; i < maxn; i++)
            p[i] = i;
    }
    int parent(int i) {
        if (p[i] == i)
            return i;
        return p[i] = parent(p[i]);
    }
    void connect(int a, int b) {
        p[parent(b)] = parent(a);
    }
    bool same(int a, int b) {
        return(parent(a) == parent(b));
    }
};