Cod sursa(job #1986698)

Utilizator ArctopusKacso Peter-Gabor Arctopus Data 28 mai 2017 20:12:30
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>

#define ll long long
#define pb push_back

using namespace std;

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

const int NLIM = 1e5 + 10;

int N, M;

int parent[NLIM];


int findParent( int x )
{
    if( parent[x] == x )
        return x;

    parent[x] = findParent( parent[x] );
    return parent[x];
}

int join( int x, int y )
{
    int px = findParent( x );
    int py = findParent( y );

    if( px != py )
    {
        parent[px] = py;
    }
}

int main()
{
    fin >> N >> M;

    for( int i = 1; i <= N; ++i )
        parent[i] = i;

    while( M-- )
    {
        int t, x, y;
        fin >> t >> x >> y;

        if( t == 1 )
        {
            join( x, y );
        }
        else
        {
            //cout << findParent( x ) << " " << findParent( y ) << "\n";
            if( findParent( x ) == findParent( y ) )
                fout << "DA\n";
            else
                fout << "NU\n";
        }
    }
    return 0;
}