Cod sursa(job #2576248)

Utilizator BogauuuBogdan Ivancu Bogauuu Data 6 martie 2020 18:05:38
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>

using namespace std;

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

int t[100005];

int rad(int x)
{
    while (t[x]>0) x=t[x];
    return x;
}

int n,f,i,q,p,x,y,rx,ry;

int main()
{
    fin >> n >> f;
    for (i=1;i<=n;i++) t[i]=-1;
    for (q=1;q<=f;q++)
    {
        fin >> p >> x >> y;
        rx=rad(x);
        ry=rad(y);
        if (p==2)
        {
            if (rx==ry) fout << "DA";
            else fout << "NU";
            fout << "\n";
            continue;
        }
        if (t[rx]=-1
            )
        {
            t[ry]=rx;
        }
        else
        {
            t[rx]=ry;
        }
    }

    return 0;
}