Cod sursa(job #3237023)

Utilizator stefan_dore_Stefan Dore stefan_dore_ Data 4 iulie 2024 09:16:59
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int NMAX = 100000;
int N, M, T[NMAX+1], H[NMAX+1];

int Find(int x) {
    if(T[x] == 0) return x;
    return T[x] = Find(T[x]);
}

void Union(int x, int y) {
    if(H[x] < H[y])
        T[x] = y;
    else {
        T[y] = x;
        if (H[x] == H[y])
            H[x]++;
    }
}

int main()
{
    int op, x, y;
    f >> N >> M;
    while(M--) {
        f >> op >> x >> y;
        x = Find(x);
        y = Find(y);
        if (op == 1) {
            if (x != y)
                Union(x, y);
        } else
            if (x == y)
                g << "DA\n";
            else
                g << "NU\n";
    }
    f.close();
    g.close();
    return 0;
}