Pagini recente » Cod sursa (job #2691392) | Cod sursa (job #615535) | Cod sursa (job #1392961) | Cod sursa (job #2365703) | Cod sursa (job #1349827)
#include <cstdio>
using namespace std;
#define MAX_N static_cast < int > ( 1e5 ) + 5
#define IN_FILE "disjoint.in"
#define OUT_FILE "disjoint.out"
int N, M;
int t[ MAX_N ], h[ MAX_N ];
inline void FindSet( int &x ) {
while( x != t[ x ] )
x = t[ x ];
}
inline void UnionSet( const int &x, const int &y ) {
if( h[ x ] == h[ y ] ) {
++h[ x ];
t[ y ] = x;
} else if( h[ x ] > h[ y ] )
t[ y ] = x;
else
t[ x ] = y;
}
int main( ) {
FILE *f, *g;
int i, op, x, y;
f = fopen( IN_FILE, "r" );
fscanf( f, "%d%d", &N, &M );
for( i = 1; i <= N; ++i ) {
t[ i ] = i;
h[ i ] = 1;
}
g = fopen( OUT_FILE, "w" );
for( i = 0; i != M; ++i ) {
fscanf( f, "%d%d%d", &op, &x, &y );
FindSet( x );
FindSet( y );
if( x != y ) {
if( op == 1 )
UnionSet( x, y );
else
fputs( "NU\n", g );
} else if( op == 2 )
fputs( "DA\n", g );
}
fclose( f );
fclose( g );
return 0;
}