Pagini recente » Cod sursa (job #2943858) | Cod sursa (job #2970826) | Cod sursa (job #496081) | Cod sursa (job #130452) | Cod sursa (job #2759073)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 100001
int t[N], h[N];
int radacina(int x)
{
if (t[x] == 0)
{
return x;
}
return radacina(t[x]);
}
void reuniune(int x, int y)
{
int rx = radacina(x);
int ry = radacina(y);
if (h[rx] < h[ry])
{
t[rx] = ry;
}
else if (h[rx] > h[ry])
{
t[ry] = rx;
}
else
{
t[rx] = ry;
h[ry]++;
}
}
bool aceeasi_multime(int x, int y)
{
return (radacina(x) == radacina(y));
}
int main()
{
FILE *in, *out;
in = fopen("disjoint.in", "r");
out = fopen("disjoint.out", "w");
int n, m, i;
fscanf(in, "%d%d", &n, &m);
for (i = 0; i < m; i++)
{
int tip, x, y;
fscanf(in, "%d%d%d", &tip, &x, &y);
if (tip == 1)
{
reuniune(x, y);
}
else
{
if (aceeasi_multime(x, y))
{
fprintf(out, "DA\n");
}
else
{
fprintf(out, "NU\n");
}
}
}
fclose(in);
fclose(out);
return 0;
}