Pagini recente » Cod sursa (job #2057435) | Cod sursa (job #2014026) | Cod sursa (job #393653) | Cod sursa (job #163909) | Cod sursa (job #272764)
Cod sursa(job #272764)
#include <cstdio>
#include <cstdlib>
class Set
{
private : int *t;
public : Set(int size)
{
t = (int *) calloc(size, sizeof(int));
}
public : void Union(int x, int y)
{
int tx = x, tmp;
while (t[tx]) tx = t[tx];
while (t[x] != tx && x != tx) { tmp = t[x]; t[x] = tx; x = tmp; }
while (y) { tmp = t[y]; t[y] = tx; y = tmp; }
}
public : int Query(int x, int y)
{
int tx = x, ty = y, tmp;
while (t[tx]) tx = t[tx];
while (t[ty]) ty = t[ty];
while (t[x] != tx && x != tx) { tmp = t[x]; t[x] = tx; x = tmp; }
while (t[y] != ty && y != ty) { tmp = t[y]; t[y] = ty; y = tmp; }
return tx == ty?1:0;
}
};
int N, M, x, y, o;
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d %d\n", &N, &M);
for ( Set set(N+1); M; --M)
{
scanf("%d %d %d\n", &o, &x, &y);
if (o-1) printf("%s\n", set.Query(x,y)?"DA":"NU");
else set.Union(x,y);
}
return 0;
}