Cod sursa(job #2946927)

Utilizator GhiciCineRazvan Dumitriu GhiciCine Data 25 noiembrie 2022 13:42:11
Problema Paduri de multimi disjuncte Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

vector<int> father(100001);

int findFather(int node) { // Functie care gaseste tatal unui nod
    if(father[node] != node)
        return findFather(father[node]);
    return node;
}

int main() {
    int n, m, operatie, x, y;

    fin >> n >> m;
    for(int i = 0; i < n; i++) // initial tatal unui nod este el insusi
        father[i] = i;

    for(int i = 0; i < m; i++) {
        fin >> operatie >> x >> y;

        if(operatie == 1)
            father[findFather(y)] = findFather(x);
        else {
            if(findFather(x) == findFather(y))
                fout << "DA" << endl;
            else fout << "NU" << endl;
        }
    }
    return 0;
}