Cod sursa(job #2147980)

Utilizator OritaLucaAndreiOrita Luca Andrei OritaLucaAndrei Data 1 martie 2018 13:54:53
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <vector>

using namespace std;
const int NMAX = 100000 ;


int t[NMAX+5] , h[NMAX+5] ;
ifstream fin("disjoint.in") ;
ofstream fout("disjoint.out") ;
int FindSet( int x )
{
    while ( x != t[x] )
        x = t[x] ;
    return x ;
}
void UnionSet ( int x , int y )
{
    if ( h[x] == h[y] )
    {
        t[y] = x ;
        h[x] ++ ;
    }
    else
        if ( h[x] > h[y] )
            t[y] = x ;
        else
            t[x] = y ;
}
int main()
{   int n , m , i , j , x , y ;
    fin >> n >> m ;
    for ( i = 1 ; i <= n ; i ++ )
    {
        t[i] = i ;
        h[i] = 1 ;
    }
    for ( i = 1 ; i <= m ; i ++ )
    {
        fin >> j >> x >> y ;
        if ( j == 1 )
        {
            if ( t[x] != t[y] )
            UnionSet ( t[x] , t[y] ) ;
        }
        else
        {
            if ( t[x] == FindSet (y))
                fout << "DA" << endl ;
            else
                fout << "NU" << endl ;
        }
    }
    return 0;
}