Pagini recente » Profil GloryGloryManUtd | Statistici Alexandru Timplaru (AlexTimplaru) | Rating Janis Peyer (Janis) | Monitorul de evaluare | Cod sursa (job #228328)
Cod sursa(job #228328)
#include <cstdio>
const int maxn = 100001;
FILE *in = fopen("disjoint.in","r"), *out = fopen("disjoint.out","w");
int n, m;
int t[maxn], h[maxn];
int find(int x)
{
if ( t[x] == x )
return x;
t[x] = find(t[x]);
return t[x];
}
void merge(int x, int y)
{
x = find(x);
y = find(y);
if ( h[x] > h[y] )
t[y] = x;
else
t[x] = y;
if ( h[x] == h[y] )
++h[y];
}
int main()
{
fscanf(in, "%d %d", &n, &m);
for ( int i = 1; i <= n; ++i )
t[i] = i;
int x, y, z;
for ( int i = 0; i < m; ++i )
{
fscanf(in, "%d %d %d", &x, &y, &z);
if ( x == 1 )
merge(y, z);
else
find(y) == find(z) ? fprintf(out, "DA\n") : fprintf(out, "NU\n");
}
return 0;
}