Pagini recente » Cod sursa (job #938479) | Cod sursa (job #2126293) | Cod sursa (job #1795200) | Cod sursa (job #2896382) | Cod sursa (job #2491798)
#include <cstdio>
using namespace std;
int t[100001], h[100001];
int FindFirstAncestor(int x) {
while(x != t[x])
x = t[x];
return x;
}
void Operation2(int x, int y) {
bool c = FindFirstAncestor(x) == FindFirstAncestor(y);
if(c)
printf("DA\n");
else
printf("NU\n");
}
void Operation1(int x, int y) {
if(h[x] == h[y]) {
t[y] = x;
h[x]++;
}
else if(h[x] > h[y]) {
t[y] = x;
}
else
t[x] = y;
}
int main() {
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w", stdout);
int n, m, op_type, x, y, tx, ty;
scanf("%d%d", &n, &m);
for(register int i = 1; i <= n; i++)
{
t[i] = i;
h[i] = 1;
}
for(register int i = 1; i <= m; i++)
{
scanf("%d%d%d", &op_type, &x, &y);
tx = FindFirstAncestor(x);
ty = FindFirstAncestor(y);
if(op_type == 2)
Operation2(x, y);
else
Operation1(tx, ty);
}
return 0;
}