Pagini recente » Cod sursa (job #504826) | Cod sursa (job #630977) | Cod sursa (job #3265262) | Cod sursa (job #2448214) | Cod sursa (job #1451476)
#include <stdio.h>
const int Dim = 100005;
int N,M,P[Dim],R[Dim];
int Find(int X)
{
int Root,aux;
for (Root = X;P[Root] != Root;Root = P[Root]);
while (P[X] != X)
{
aux = P[X];
P[X] = Root;
X = aux;
}
return Root;
}
void Union(int X,int Y)
{
if (R[X] > R[Y])
P[Y] = X;
else
P[X] = Y;
if (R[X] == R[Y])
R[Y]++;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&N,&M);
for (int i = 1;i <= N;i++)
{
P[i] = i;
R[i] = 1;
}
int Op,X,Y;
for (int i = 1;i <= M;i++)
{
scanf("%d%d%d",&Op,&X,&Y);
int A = Find(X),B = Find(Y);
if (Op == 1)
{
if (A != B)
Union(A,B);
}
else
{
if (A == B)
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}