Pagini recente » Cod sursa (job #2160102) | Cod sursa (job #348955) | Cod sursa (job #656213) | Cod sursa (job #609382) | Cod sursa (job #323012)
Cod sursa(job #323012)
#include <stdio.h>
#include <stdlib.h>
int * newSet(int n)
{
int * S = (int *)calloc( (n + 1), sizeof(int));
return S;
}
void deleteSet( int * S)
{
if(S == NULL)
return;
free( S );
S = NULL;
}
int findSet(int * S, int x)
{
if(S[x] <= 0)
return x;
return S[x] = findSet(S, S[x]);//compresia caii
}
void unionSet(int * S, int x, int y)
{
x = findSet(S, x);
y = findSet(S, y);
if(x == y)
return;
if(-S[x] > -S[y])
S[y] = x;
else{
if(S[x] == S[y])
S[y]--;
S[x] = y;
}
}
char *REZ[] = {"NU", "DA"};
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int n, m, c, x, y;
scanf("%d %d", &n, &m);
int * S = newSet( n );
for(;m--;){
scanf("%d %d %d", &c, &x, &y);
if(c == 1)
unionSet(S, x, y);
else
printf("%s\n", REZ[findSet(S, x) == findSet(S, y)]);
}
deleteSet( S );
return 0;
}