Pagini recente » Cod sursa (job #3235593) | Cod sursa (job #3270557) | Cod sursa (job #230789) | Borderou de evaluare (job #2245280) | Cod sursa (job #1906042)
#include <cstdio>
using namespace std;
int t[1005] , h[1005];
void unionMD(int tx , int ty)
{
if(h[tx] == h[ty])
{
h[tx] ++;
t[ty] = tx;
}
else if(h[tx] > h[ty])
t[ty] = tx;
else
t[tx] = ty;
}
int findMD(int x)
{
while(x != t[x])
x = t[x];
return x;
}
int main()
{
freopen("disjoint.in" , "r" , stdin);
freopen("disjoint.out" , "w" , stdout);
int x , y , n , nr , i , tx , ty , m , cnt;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= n ; i ++)
t[i] = i , h[i] = 1;
while(scanf("%d%d%d" , &cnt , &x , &y) != EOF)
{
tx = findMD(x);
ty = findMD(y);
if(cnt == 1)
unionMD(tx , ty);
else
{
if(tx == ty)
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}