Pagini recente » Cod sursa (job #159519) | Istoria paginii utilizator/[email protected] | Cod sursa (job #1860582) | Cod sursa (job #3298875) | Cod sursa (job #2036481)
// paduri de multimi disjuncte.cpp : Defines the entry point for the console application.
//
#include <stdlib.h>
#include <stdio.h>
#define MAX_SIZE 100003
int N, M, type, x, y;
int rank[MAX_SIZE], father[MAX_SIZE];
inline int find(int x)
{
while (x != father[x])
{
x = father[x];
}
return x;
}
inline void join(int x, int y)
{
if (rank[x] < rank[y])
{
father[x] = y;
}
else
{
father[y] = x;
}
if (rank[x] == rank[y])
{
++rank[y];
}
}
int main()
{
freopen("disjoint.in", "r", stdin);
scanf("%d %d", &N, &M);
for (int i = 1; i <= N; ++i)
{
rank[i] = 1;
father[i] = i;
}
freopen("disjoint.out", "w", stdout);
//int type, x, y;
while (M--)
{
scanf("%d %d %d", &type, &x, &y);
int rx = find(x), ry = find(y);
if (type == 1)
{
join(rx, ry);
}
else
{
printf ( (rx == ry ? "DA\n" : "NU\n") );
}
}
fclose(stdin);
fclose(stdout);
return 0;
}