Cod sursa(job #902950)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 1 martie 2013 17:39:00
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <cstdio>
#include <cstring>
#include <cassert>

#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>

using namespace std;

typedef long long LL;
typedef vector<int>::iterator it;

const int oo = 0x3f3f3f3f;
const int MAX_N = 100005;
const int NIL = -1;

int N, Father[MAX_N];

int Find(int X) {
    if (Father[X] == NIL)
        return X;
    Father[X] = Find(Father[X]);
    return Father[X];
}

inline bool Merge(int X, int Y) {
    X = Find(X); Y = Find(Y);
    if (X == Y)
        return false;
    Father[Y] = X;
    return true;
}

void InitForest() {
    memset(Father, NIL, sizeof(Father));
}

int main() {
    assert(freopen("disjoint.in", "r", stdin));
    assert(freopen("disjoint.out", "w", stdout));
    int NQ; assert(scanf("%d %d", &N, &NQ) == 2);
    InitForest();
    for (; NQ > 0; --NQ) {
        int Type, X, Y; assert(scanf("%d %d %d", &Type, &X, &Y) == 3);
        if (Type == 1)
            assert(Merge(X, Y));
        else
            printf("%s\n", (Find(X) == Find(Y) ? "DA" : "NU"));
    }
    return 0;
}