Cod sursa(job #1320138)

Utilizator superman_01Avramescu Cristian superman_01 Data 17 ianuarie 2015 17:25:15
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <algorithm>

#define NMAX 100005

using namespace std;

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

int N , M ;
int Father[NMAX];

int  Find ( int X ){
    int i , j , R;
    for( R = Father[X] ; R!=Father[R] ; R = Father[R] );
    return R;
}
void Unite ( int X , int Y ){
    Father[X] = Y ;
    return ;
}
int main ( void ){
  int i , j ;
  in >> N >> M ;
  for ( i = 1 ; i <= N ; ++i )
     Father[i] = i ;
  for ( i = 1 ; i <= M ; ++i ){
      int type , x , y;
      in >> type >> x >> y;
        if ( type ==  1 )
        Unite ( Find(x) , Find(y) );
        else out << ( Find(x) != Find(y) ? "NU" : "DA" );
  }
  return 0;
}