Cod sursa(job #1199660)

Utilizator lorundlorund lorund Data 20 iunie 2014 09:19:57
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <cstdio>
#define SIZE 100005

int N, M;
int parent[SIZE];

int find(int x){
    while (parent[x]!=x){
        x = parent[x];
    }
    return x;
}

void merge(int x, int y){
    parent[find(x)] = find(y);
}

int main()
{
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);

    scanf("%d%d", &N, &M);
    for (int i=1; i<=N; ++i){
        parent[i] = i;
    }
    for (int i=1; i<=M; ++i){
        int op, x, y;

        scanf("%d%d%d", &op, &x, &y);
        switch (op){
        case 1:
            merge(x, y);
            break;
        case 2:
            printf("%s\n", find(x)==find(y) ? "DA" : "NU");
        }
    }
    return 0;
}