Pagini recente » Cod sursa (job #1804962) | Cod sursa (job #2083523) | Cod sursa (job #2107723) | Cod sursa (job #2943608) | Cod sursa (job #2032681)
#include <iostream>
#include <cstdlib>
using namespace std;
int t[100001], h[100001];
int _find(int x)
{
while(t[x] != x)
x = t[x];
return x;
}
void _union(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, p, x, y;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
{
t[i] = i;
h[i] = 1;
}
for(int i = 0; i < m; i++)
{
scanf("%d%d%d", &p, &x, &y);
if(p == 1)
{
_union(_find(x), _find(y));
}else if(p == 2)
{
if(_find(x) != _find(y))
{
printf("%s\n", "NU");
}else
{
printf("%s\n", "DA");
}
}
}
return 0;
}