Pagini recente » Cod sursa (job #3297524) | Clasamentul arhivei ACM | Cod sursa (job #3297320)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5;
int father[NMAX + 1];
int siz[NMAX + 1];
int root( int a ) {
if ( father[a] == a ) {
return a;
}
return ( father[a] = root( father[a] ) );
}
void unite( int a, int b ) {
a = root( a );
b = root( b );
if ( siz[a] < siz[b] ) {
swap( a, b );
}
father[b] = a;
siz[a] += siz[b];
}
int main() {
ifstream fin( "disjoint.in" );
ofstream fout( "disjoint.out" );
int n, m;
fin >> n >> m;
for ( int i = 1; i <= n; i ++ ) {
father[i] = i;
siz[i] = 1;
}
for ( int i = 1, tip, a, b; i <= m; i ++ ) {
fin >> tip >> a >> b;
if ( tip == 1 ) {
unite( a, b );
} else {
if ( root( a ) == root( b ) ) {
fout << "DA\n";
} else {
fout << "NU\n";
}
}
}
return 0;
}