Cod sursa(job #2924515)

Utilizator alexboat10759Alex Mateescu alexboat10759 Data 4 octombrie 2022 08:58:24
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>

using namespace std;

int dsu[100005], tata = 0;

void find_tata(int nod)
{
    if(nod != dsu[nod])
    {
        find_tata(dsu[nod]);
        dsu[nod] = tata;
    }
    else
    {
        tata = nod;
    }
}

int main()
{
    ifstream cin("disjoint.in");
    ofstream cout("disjoint.out");

    int n, m;
    cin>>n>>m;
    for(int i = 1; i <= n; i++)
        dsu[i] = i;
    for(int i = 1; i <= m; i++)
    {
        int op, a, b;
        cin>>op>>a>>b;

        if(op == 1)
        {
            find_tata(a);
            find_tata(b);
            dsu[a] = tata;
        }
        else
        {
            int tata1, tata2;
            find_tata(a);
            tata1 = tata;
            find_tata(b);
            tata2 = tata;

            if(tata1 == tata2)
                cout<<"DA"<<'\n';
            else
                cout<<"NU"<<'\n';
        }

    }
    return 0;
}