Cod sursa(job #2845282)

Utilizator LuciBBadea Lucian LuciB Data 7 februarie 2022 18:18:30
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 fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 1e5;

int sef[NMAX + 1];

int findd(int x) {
    if(sef[x] == x)
        return x;
    return (sef[x] = findd(sef[x]));
}

void unionn(int x, int y){
    int sefx, sefy;
    sefx = findd(x);
    sefy = findd(y);
    sef[sefx] = sefy;
}


int main() {
    int n, q;
    fin >> n >> q;
    for(int i = 1; i <= n; i++)
        sef[i] = i;
    while(q--) {
        int caz, x, y;
        fin >> caz >> x >> y;
        if(caz == 1) {
            unionn(x, y);
        } else {
            if(findd(x) == findd(y)) {
                fout << "DA";
            } else {
                fout << "NU";
            }
            fout << '\n';
        }
    }
    return 0;
}