Cod sursa(job #2117719)

Utilizator ifrimencoAlexandru Ifrimenco ifrimenco Data 29 ianuarie 2018 12:06:27
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
#define nmax 100005

using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");


int tata[nmax], h[nmax];

void unire (int x, int y) {
    if (h[x] > h[y]) tata[y] = x;
    else tata[x] = y;
    if (x[h] == h[y]) h[y]++;
}
int gasire (int x) {
    int r = x;
    while (tata[r]) r = tata[r];
    int y = x, t;
    while (y != r) {
        t = tata[y];
        tata[y] = r;
        y = t;
    }
    return r;
}
int main()
{
    int n, m, x, y;
    f >> n >> m;
    int q;
    for (int i = 1; i <= m; ++i) {
        f >> q >> x >> y;
        if (q == 1) unire (gasire(x), gasire(y));
        else {
            if (gasire(x) != gasire(y)) g << "NU\n";
            else g << "DA\n";
        }
    }
    return 0;
}