Cod sursa(job #2278029)

Utilizator Dragomiralexandru621@yahoo.comDragomir ionut alexandru [email protected] Data 7 noiembrie 2018 10:33:52
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in") ;
ofstream g("disjoint.out") ;
int m , n , i , j , a[100001];

int grad( int b )
{
    if( a[b] == b )
        return b ;
    else grad( a[b] ) ;
}

int main()
{

f >> m >> n ;
for( i = 1 ; i <= m ; i ++ ){
    a[i] = i ;
}
for( i = 1 ; i <= n ; i ++ )
{  int cod , x , y ;
    f >> cod >> x >> y ;

    if( cod == 1){

    a[x] = grad( y ) ;
    }
    else
        if( grad( x ) == grad( y ) )
            g << "DA" << "\n" ;
        else g << "NU" << "\n" ;
}
return 0;
}