Pagini recente » Cod sursa (job #2417338) | Cod sursa (job #916993) | Cod sursa (job #44473) | Cod sursa (job #2456865) | Cod sursa (job #1809823)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in( "disjoint.in" ) ;
ofstream out( "disjoint.out" ) ;
int r[ 100001 ] , t[ 100001 ] ;
int findd ( int x )
{
int rad = x ;
while( t[ rad ] != 0 )
rad = t[ rad ] ;
int tmp ;
while( t[ x ] != 0 )
{
tmp = t[ x ] ;
t[ x ] = tmp ;
x = rad ;
}
return rad ;
}
void uniune( int x , int y )
{
if( r[ x ] > r[ y ] )
t[ y ] = x ;
else
if( r[ x ] < r [ y ] )
t[ x ] = y ;
else
{
t[ y ] = x ;
r[ x ] ++;
}
}
int main()
{
int N , M , q , x , y , i ;
in >> N >> M ;
for( i = 1 ; i <= M ; i ++)
{
in >> q >> x >> y ;
if( q == 1 )
uniune( x , y ) ;
else
if( findd( x ) == findd( y ) )
out << "DA" << '\n' ;
else
out << "NU" << '\n' ;
}
return 0;
}