Cod sursa(job #2945603)

Utilizator JovialJokerFlorea George JovialJoker Data 23 noiembrie 2022 22:26:04
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
using namespace std;

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

vector<int> tata(100001), rang(100001);
int N, M;

int findAncestor(int k){
    ///we recursively traverse the fathers vector to find the one node that doesn't have a father
    if(tata[k]) {
        tata[k] = findAncestor(tata[k]);
        return tata[k];
    }
    return k;
}

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