Cod sursa(job #2670933)

Utilizator As932Stanciu Andreea As932 Data 10 noiembrie 2020 22:49:27
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

const int nMax = 1e5 + 5;

int n, m;
int rankk[nMax], root_node[nMax];

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

    return root_node[nod];
}

void make_union(int root1, int root2){
    if(rankk[root1] > rankk[root2])
        root_node[root2] = root1;
    else
        root_node[root1] = root2;

    if(rankk[root1] == rankk[root2])
        rankk[root2]++;
}

int main()
{
    fin >> n >> m;

    for(int i = 1; i <= n; i++){
        rankk[i] = 1;
        root_node[i] = i;
    }

    for(int i = 1; i <= m; i++){
        int tip, x, y;

        fin >> tip >> x >> y;

        if(tip == 1)
            make_union(root(x), root(y));
        else {
            if(root(x) == root(y))
                fout << "DA\n";
            else
                fout << "NU\n";
        }
    }

    return 0;
}