Pagini recente » Cod sursa (job #1996603) | Cod sursa (job #2308609) | Cod sursa (job #2377705) | Cod sursa (job #2169869) | Cod sursa (job #686680)
Cod sursa(job #686680)
#include<cstdio>
#include<set>
#include<ctime>
#include<vector>
#define NMAX 100010
#define INfile "disjoint.in"
#define OUTfile "disjoint.out"
using namespace std ;
vector < int > H ( NMAX ) ;
int N , M ;
void initialise () ;
void solve () ;
void task1 ( int x , int y ) ;
void task2 ( int x , int y ) ;
int main ()
{
freopen ( INfile , "r" , stdin ) ;
freopen ( OUTfile , "w" , stdout ) ;
//double ss , tt ;
//ss = clock () ;
solve () ;
//tt = clock () ;
//printf ( "%lf" , ( tt - ss ) / CLK_TCK ) ;
return 0 ;
}
void initialise ()
{
int i ;
for ( i = 1 ; i <= N ; ++ i )
{
H [ i ] = i ;
//P [ i ].insert ( i ) ;
}
}
void solve ()
{
int ind , cod , x , y;
scanf ( "%d %d" , & N , & M ) ;
initialise () ;
for ( ind = 1 ; ind <= M ; ++ ind )
{
scanf ( "%d %d %d" , & cod , & x , & y) ;
if ( ind == 24340 )
cod = cod ;
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 ( x != H [ x ] )
{
x = H [ x ] ;
++ nrx ;
}
while ( y != H [ y ] )
{
y = H [ y ] ;
++ nry ;
}
if ( nrx >= nry )
H [ yi ] = x ;
else
H [ xi ] = y ;
/*
if ( P [ H [ x ] ].size () < P [ H [ y ] ].size () )
{
//P [ H [ y ] ].insert ( P [ H [ x ] ] .begin (), P [ H [ x ] ] .end () ) ;
while ( x != H [ x ] )
x =
H [ x ] = H [ y ] ;
}
else
{
//P [ H [ x ] ].insert ( P [ H [ y ] ] .begin (), P [ H [ y ] ] .end () ) ;
H [ y ] = H [ x ] ;
}
*/
}
void task2 ( int x , int y )
{
while ( x != H [ x ] )
x = H [ x ] ;
while ( y != H [ y ] )
y = H [ y ] ;
if ( H [ x ] == H [ y ] )
printf ( "DA\n" ) ;
else
printf ( "NU\n" ) ;
}