Pagini recente » Cod sursa (job #2317043) | Cod sursa (job #2838084)
#include <fstream>
#include <cstring>
#define NMAX 100005
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int father[NMAX];
int g[NMAX];
int Find(int i)
{
while (father[i] != i)
{
i = father[i];
}
return i;
}
void Union(int a, int b)
{
a = Find(a);
b = Find(b);
if (g[a] > g[b])
swap(a, b);
father[a] = b;
g[b] = g[b] + g[a];
}
int main()
{
int n, m, i, j, op, x, y;
in >> n >> m;
for (i = 1; i < NMAX; ++i)
{
father[i] = i;
g[i] = 1;
}
for (i = 1; i <= m; ++i)
{
in >> op >> x >> y;
if (op == 1)
{
Union(x, y);
}
else if (op == 2)
{
if (Find(x) == Find(y))
out << "DA" << '\n';
else
out << "NU" << '\n';
}
}
return 0;
}