Pagini recente » Cod sursa (job #2160173) | Cod sursa (job #2368873) | Cod sursa (job #1374711) | Cod sursa (job #2089138) | Cod sursa (job #1704672)
//package disjoint;
import java.util.*;
import java.io.*;
/***
*
* @author Patrick
*
*/
public class Main {
public static int [ ] tata = new int [ 100999 ] ;
public static int [ ] rang = new int [ 100999 ] ;
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new BufferedInputStream(new FileInputStream("disjoint.in")));
PrintStream output = new PrintStream(new FileOutputStream("disjoint.out"));
int N = scanner.nextInt() ;
int M = scanner.nextInt() ;
for ( int i = 1 ; i <= N ; ++ i ) {
tata [ i ] = i ;
rang [ i ] = 1 ;
}
for ( int i = 1 ; i <= M ; ++ i ) {
int Tip = scanner.nextInt() ;
if ( Tip == 1 ) {
int X = scanner.nextInt() ;
int Y = scanner.nextInt() ;
unite ( X , Y ) ;
}
else {
int X = scanner.nextInt() ;
int Y = scanner.nextInt() ;
if ( stramos ( X ) == stramos ( Y ) ) {
output.print( "DA\n" );
}
else {
output.print( "NU\n" );
}
}
}
}
public static int stramos ( int nod )
{
int R = nod ;
while ( R != tata [ R ] ) {
R = tata [ R ] ;
}
while ( nod != tata [ nod ] ) {
int aux = tata [ nod ] ;
tata [ nod ] = R ;
nod = aux ;
}
return R ;
}
public static void unite ( int x , int y )
{
x = stramos ( x ) ;
y = stramos ( y ) ;
if ( x == y ) {
return ;
}
if ( rang [ x ] > rang [ y ] ) {
rang [ x ] += rang [ y ] ;
tata [ y ] = tata [ x ] ;
}
else {
rang [ y ] += rang [ x ] ;
tata [ x ] = tata [ y ] ;
}
}
}