Cod sursa(job #3363474)

Utilizator Cyb3rBoltSbora Ioan-David Cyb3rBolt Data 18 august 2026 15:15:11
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int DIM = 1e5;
int n, tt, tata[DIM + 3], rang[DIM + 3];

inline int findTata(int nod) {
    while(tata[nod] != nod) nod = tata[nod];
    return nod;
}

inline void unireNoduri(int x, int y) {
    if(rang[x] < rang[y]) tata[x] = y;
    else if(rang[y] < rang[x]) tata[y] = x;
    else tata[x] = y, rang[y]++;
}

int main()
{
    int tt; fin >> n >> tt;
    for(int i=1; i<=n; i++) tata[i] = i, rang[i] = 1;
    while(tt--) {
        int tip, x, y; fin >> tip >> x >> y;
        if(tip == 1) {
            int tataX = findTata(x);
            int tataY = findTata(y);
            if(tataX != tataY) unireNoduri(tataX, tataY);
        }
        else {
            int tataX = findTata(x);
            int tataY = findTata(y);
            if(tataX == tataY) fout << "DA\n";
            else fout << "NU\n";
        }
    }

    return 0;
}