Pagini recente » Cod sursa (job #1421798) | Cod sursa (job #919449) | Cod sursa (job #2467477) | Cod sursa (job #1987977) | Cod sursa (job #1522988)
#include <cstdio>
#define in "disjoint.in"
#define out "disjoint.out"
#define NMAX 100007
using namespace std;
int n, m, cod, a, b, tata[NMAX];
int root(const int &key)
{
int rkey = key, aux;
while(tata[rkey] > 0)
rkey = tata[rkey];
aux = key;
while(tata[aux] > 0)
{
int tmp = aux;
aux = tata[aux];
tata[tmp] = rkey;
}
return rkey;
}
void unit(const int &x, const int &y)
{
int rx = root(x), ry = root(y);
if(tata[rx] < tata[ry])
{
tata[rx] += tata[ry];
tata[ry] = rx;
}
else
{
tata[ry] += tata[rx];
tata[rx] = ry;
}
}
int query(const int &x, const int &y)
{
int rx = root(x), ry = root(y);
return (rx == ry);
}
int main()
{
freopen(in , "r", stdin);
freopen(out, "w", stdout);
scanf("%d %d", &n, &m);
for(int i = 1; i<= n; ++i) tata[i] = -1;
for(; m; --m)
{
scanf("%d %d %d", &cod, &a, &b);
if(cod == 1) unit(a, b);
if(cod == 2)
{
if(query(a, b)) printf("DA\n");
else printf("NU\n");
}
}
return 0;
}