Cod sursa(job #2852946)

Utilizator Alle43221Moroz Alexandra-Ioana Alle43221 Data 19 februarie 2022 18:43:19
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int T[100001];
int n, m, x, y, c, tx, ty;

int det_tata(int x)
{
    if(T[x]==0)
    {
        return x;
    }
    T[x]=det_tata(T[x]);
    return T[x];
}

int main()
{
    fin>>n>>m;
    for(int i=0; i<m; i++)
    {
        fin>>c>>x>>y;
        tx=det_tata(x);
        ty=det_tata(y);
        if(c==2)
        {
            if(tx!=ty)
            {
                fout<<"NU"<<'\n';
            }
            else
            {
                fout<<"DA"<<'\n';
            }
        }
        else
        {
            T[tx]=ty;
        }
    }
    fin.close();
    fout.close();
    return 0;
}