Pagini recente » Cod sursa (job #237974) | Cod sursa (job #2130117) | Cod sursa (job #2023568) | Cod sursa (job #2207366) | Cod sursa (job #1244912)
#include<fstream>
using namespace std;
ifstream in( "disjoint.in" );
ofstream out( "disjoint.out" );
const int NMAX = 100000;
int N,M;
int tt[NMAX+1], gr[NMAX+1];
int findy( int x ) {
if ( tt[x] == x ) return x;
else return ( tt[x] = findy( tt[x] ) );
}
void Next_i32( int &nr ) {
nr= 0;
char c;
in.get(c);
while( c<48 || c>58 ) in.get(c);
while( c>47 && c<59 ) {
nr= nr*10+c-48;
in.get(c);
}
}
int main() {
in >> N >> M; in.get();
for( int i= 1; i<=N; ++i ) tt[i]= i, gr[i] = 1;
for( int i= 1; i<=M; ++i ) {
int op, A,B, tA,tB;
Next_i32(op); Next_i32(A); Next_i32(B);
tA= findy( A );
tB= findy( B );
if ( op == 1 ) {
if ( gr[tA] < gr[tB] ) {
gr[tB]+= gr[tA];
tt[tA]= tB;
}
else {
gr[tA]+= gr[tB];
tt[tB]= tA;
}
}
else {
if ( tA == tB ) out << "DA" << '\n';
else out << "NU" << '\n';
}
}
return 0;
}