Cod sursa(job #3144423)

Utilizator xDemonstyMatei Haba Ionut xDemonsty Data 8 august 2023 02:10:37
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>

using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");

long long rad [ 100005 ];

int get( int nod )
{
    int aux = nod ;

    while ( rad [ nod ] > 0 )
    {
        nod = rad [ nod ] ;
    }
    int root= nod ;

    nod = aux ;

    while ( nod != root )
    {

        aux = rad [ nod ] ;
        rad [ nod ] = root;
        nod = aux ;

    }

    return root ;
}
void unite(int a, int b )
{
    int x = get (  a);
    int y = get ( b );

    if ( x == y )
        return ;

    if ( rad [ x ] < rad [ y ])
    {
        rad [ x ] += rad [ y ] ;
        rad [ y ] = x ;
    }
    else
    {
        rad [ y ] += rad [x ];
        rad [ x ] = y;
    }
}


bool same_find( int  a, int  b )
{
    return ( get ( a ) == get( b )) ;
}
int main()
{
    int q, n  ;
    cin >> n ;

    for ( int i = 1; i <= n ; i ++ )
        rad [i ] = -1 ;
    cin >> q;

    for ( int i = 1; i <= q ; i ++ )
    {
        int tip, a, b;
        cin >> tip >>  a>> b ;
        if ( tip == 1 )
        {
            unite(a,b);
        }
        else
        {
            if ( same_find( a, b) == true )
                cout << "DA"<< '\n';
            else
                cout << "NU" << '\n';
        }

    }
    return 0;
}