Cod sursa(job #3293683)

Utilizator robert_dumitruDumitru Robert Ionut robert_dumitru Data 12 aprilie 2025 11:55:01
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, Q;
int t[100005];

void Union(int x, int y)
{
    t[x] = y;
}
int FindRoot(int x)
{
    int y, rad = x;
    while (t[rad] != 0)
        rad = t[rad];
    while (x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}

int main()
{
    int op, x, y;
    fin >> n >> Q;
    while (Q--)
    {
        fin >> op >> x >> y;
        if (op == 1) Union(FindRoot(x), FindRoot(y));
        else fout << (FindRoot(x) == FindRoot(y) ? "DA\n" : "NU\n");
    }
    return 0;
}