IOIBIN - Các thùng nước

Tác giả: ladpro98

Ngôn ngữ: C++

//http://vn.spoj.com/problems/IOIBIN/
#include <cstdio>
const int N = 10005;
using namespace std;

struct disjoint_set {
    int P[N];
    disjoint_set() {for(int i = 0; i < N; i++) P[i] = i;}
    int at(int x) {return (x == P[x]) ? P[x] : (P[x] = at(P[x]));}
    void join(int x, int y) {P[at(x)] = at(y);}
};
disjoint_set Lab;

int main()
{
    int x, y, z, q;
    scanf("%d", &q);
    while (q--) {
        scanf("%d %d %d", &x, &y, &z);
        x = Lab.at(x); y = Lab.at(y);
        if (z == 1) {if (x != y) Lab.join(x, y);}
        else {printf("%d\n", x == y ? 1 : 0);}
    }
    return 0;
}

Download