Cod sursa(job #1621709)

Utilizator felipeGFilipGherman felipeG Data 29 februarie 2016 21:01:24
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#define Nmax 100001
using namespace std;

ifstream f ("disjoint.in");
ofstream g ("disjoint.out");

int n, m, t[ Nmax ];

int multime ( int a )
{
    if ( t[ a ] == a)
        return a;

    t[ a ] = multime ( t[ a ] );
    return t[ a ];
}

void unificare ( int a, int b )
{
    int x, y;

    x = multime( a );
    y = multime( b );
    t[ x ] = y;
}

void run ()
{
    int x, y, op;

    f >> n >> m;

    for ( int i = 1; i <= n; ++ i )
        t[ i ] = i;

    for ( int i = 1; i <= m; ++ i )
    {
        f >> op >> x >> y;

        if ( op == 1 )
            unificare( x, y );

        else
        {
            if ( multime ( x ) == multime( y ) )
                g << "DA" << endl;
            else
                g << "NU" << endl;
        }
    }
}

int main()
{
    run ();
    return 0;
}