Cod sursa(job #3343613)

Utilizator robertcosacCosac Robert-Mihai robertcosac Data 27 februarie 2026 20:33:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int dim[100009], repr[100009];
int get_repr (int x)
{
    if (x==repr[x])
        return x;
    repr[x]=get_repr (repr[x]);
    return repr[x];
}
void join (int x, int y)
{
    x=get_repr(x), y=get_repr(y);
    if (x==y) return;
    if (dim[x]<dim[y])
        swap (x, y);
    dim[x]+=dim[y];
    repr[y]=repr[x];
}
signed main ()
{
    int n;
    f >> n;
    for (int i=1; i<=n; i++)
        dim[i]=1, repr[i]=i;
    int q;
    f >> q;
    while (q--)
    {
        int tip, x, y;
        f >> tip >> x >> y;
        if (tip==1)
            join (x, y);
        else
        {
            x=get_repr(x), y=get_repr(y);
            if (x==y)
                g <<"DA\n";
            else
                g << "NU\n";
        }
    }
}