Cod sursa(job #3232663)

Utilizator TomMMMMatei Toma TomMMM Data 31 mai 2024 20:30:23
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int N_max = 100005;
int n, m;
int father[N_max], Size[N_max];
void construire(){
    for(int i = 1; i <= n; i++){
        father[i] = i;
        Size[i] = 1;
    }
}
int supreme_father(int x){
    if(father[x] == x) return x;
    father[x] = supreme_father(father[x]);
    return father[x];
}

int main() {
    fin >> n >> m;
    construire();
    int tip, x1, x2;
    for(int i = 1; i <= m; i++) {
        fin >> tip >> x1 >> x2;
        int f1 = supreme_father(x1);
        int f2 = supreme_father(x2);
        if(tip == 2){
            if(f1 == f2)fout << "DA" << '\n';
            else        fout << "NU" << '\n';
            continue;
        }else{
            if(f1 != f2){
                if(Size[f1] > Size[f2]){
                    father[f2] = f1;
                    Size[f1] += Size[f2];
                }else{
                    father[f1] = f2;
                    Size[f2] += Size[f1];
                }
            }
        }
    }
}