Cod sursa(job #1380129)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 6 martie 2015 22:13:21
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define DEBUG 1
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif

#define MAXN 100010

FILE *in = fopen("disjoint.in", "r");
FILE *out = fopen("disjoint.out", "w");

int n, m;
int cod, xx, yy;
pair<int, int> disj[MAXN];

int findR(int x) {
    if(disj[x].fs != x)
        disj[x].fs = findR(disj[x].fs);

    return disj[x].fs;
}

void unit(int x, int y) {
    int xRoot = findR(x);
    int yRoot = findR(y);

    if(disj[xRoot].sc < disj[yRoot].sc) {
        disj[xRoot].fs = yRoot;
    } else if(disj[yRoot].sc < disj[xRoot].sc) {
        disj[yRoot].fs = xRoot;
    } else {
        disj[yRoot].fs = xRoot;
        disj[xRoot].sc++;
    }
}

int main() {
    fscanf(in, "%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        disj[i].fs = i;
        disj[i].sc = 0;
    }

    for (int i = 0; i < m; ++i) {
        fscanf(in, "%d%d%d", &cod, &xx, &yy);
        if(cod == 1)
            unit(xx, yy);
        else
            fprintf(out, "%s\n", ((findR(xx) == findR(yy)) ? "DA" : "NU"));
    }



    return 0;
}