Cod sursa(job #2715556)

Utilizator As932Stanciu Andreea As932 Data 3 martie 2021 20:45:12
Problema Paduri de multimi disjuncte Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
//infoarena.ro/problema/disjoint
#include <fstream>

using namespace std;

ifstream cin("disjoint.in");
ofstream cout("disjoint.out");

const int nMax = 1e5;

int n, m, rnk[nMax], rt[nMax];

void read(){
    cin >> n >> m;

    for(int i = 1; i <= n; i++){
        rnk[i] = 1;
        rt[i] = i;
    }
}

int root(int nod){
    if(rt[nod] != nod)
        rt[nod] = root(rt[nod]);

    return rt[nod];
}

void uniune(int x, int y){
    if(rnk[x] > rnk[y])
        rt[y] = x;
    else
        rt[x] = y;

    if(rnk[x] == rnk[y])
        rnk[y]++;
}

void operations(){
    int cod, x, y;
    for(int i = 1; i <= m; i++){
        cin >> cod >> x >> y;

        if(cod == 1)
            uniune(root(x), root(y));
        else {
            if(root(x) == root(y))
                cout << "DA\n";
            else
                cout << "NU\n";
        }
    }
}

int main()
{
    read();
    operations();

    return 0;
}