Pagini recente » Cod sursa (job #2046380) | Cod sursa (job #2753637) | Istoria paginii runda/daupentrumata | Istoria paginii runda/123_123/clasament | Cod sursa (job #1669029)
#include <cstdio>
#include <algorithm>
using namespace std;
int const NMAX = 100000;
int t[NMAX + 5];
int h[NMAX + 5];
int FindSet(int x) {
while( t[x] != x )
x = t[x];
return x;
}
void UnionSet( int x, int y ) {
if( h[x] > h[y] )
t[y] = x;
else if( h[y] > h[x] )
t[x] = y;
else h[x]++, t[y] = x;
}
int main() {
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int n, m, cod, x, y, i;
scanf("%d%d", &m, &n);
for( i = 1; i <= n; i++ )
t[i] = i, h[i] = 1;
for( i = 1; i <= n; i++ ) {
scanf("%d%d%d", &cod, &x, &y);
if( cod == 1 ) {
x = FindSet(x), y = FindSet(y);
UnionSet( x, y );
} else if( cod == 2 ) {
if( FindSet(x) == FindSet(y) )
printf("DA\n");
else printf("NU\n");
}
}
return 0;
}