Pagini recente » Cod sursa (job #2899850) | Cod sursa (job #2885823) | Cod sursa (job #27716) | Cod sursa (job #267229) | Cod sursa (job #686765)
Cod sursa(job #686765)
#include<cstdio>
#include<set>
#include<ctime>
#include<vector>
#define NMAX 100010
#define INfile "disjoint.in"
#define OUTfile "disjoint.out"
using namespace std ;
int H [ NMAX ] ;
int N , M ;
void solve () ;
void task1 ( int x , int y ) ;
void task2 ( int x , int y ) ;
int main ()
{
freopen ( INfile , "r" , stdin ) ;
freopen ( OUTfile , "w" , stdout ) ;
solve () ;
return 0 ;
}
void solve ()
{
int ind , cod , x , y , nr = 0 ;
scanf ( "%d %d" , & N , & M ) ;
for ( ind = 1 ; ind <= M ; ++ ind )
{
scanf ( "%d %d %d" , & cod , & x , & y) ;
if ( cod == 1 )
task1 ( x , y ) ;
else
task2 ( x , y ) ;
}
}
void task1 ( int x , int y )
{
int nrx = 1 , nry = 1 ;
int xi = x , yi = y ;
while ( H [ x ] )
{
x = H [ x ] ;
++ nrx ;
}
while ( H [ y ] )
{
y = H [ y ] ;
++ nry ;
}
if ( nrx >= nry )
H [ y ] = x ;
else
H [ x ] = y ;
}
void task2 ( int x , int y )
{
while ( H [ x ] )
x = H [ x ] ;
while ( H [ y ] )
y = H [ y ] ;
if ( x == y )
printf ( "DA\n" ) ;
else
printf ( "NU\n" ) ;
}