Cod sursa(job #1416373)

Utilizator Emilia26Hangan Emilia Emilia26 Data 7 aprilie 2015 21:39:24
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>
using namespace std;

ifstream is("disjoint.in");
ofstream os("disjoint.out");

#define MAX 100002

int n, m, a[MAX], c, x, y;

void SetRoot()
{
    for ( int i = 1; i <= m; ++i )
        a[i] = i;
}

int GetRoot( int x )
{
    if ( a[x] == x )
        return x;
    int root = GetRoot(a[x]);
    a[x] = root;
    return root;
}

int main()
{
    is >> n >> m;
    SetRoot();
    for ( int i = 1; i <= m; ++i )
    {
        is >> c >> x >> y;
        if ( c == 1 )
            a[GetRoot(x)] = GetRoot(y);
        else
        {
            if( GetRoot(x) == GetRoot(y) )
                os << "DA\n";
            else
                os << "NU\n";
        }
    }

    is.close();
    os.close();
    return 0;
}