Pagini recente » Cod sursa (job #1502256) | Cod sursa (job #335254) | Cod sursa (job #2161919) | Cod sursa (job #2700377) | Cod sursa (job #3138272)
#include <iostream>
#include <fstream>
#define N 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[N], h[N], n, m;
int Find(int node)
{
if (t[node] != node)
t[node] = Find(t[node]);
return t[node];
}
void Union(int A, int B)
{
int rootA = Find(A);
int rootB = Find(B);
if (h[rootA] > h[rootB])
{
t[rootB] = rootA;
h[rootA] = max(h[rootA], h[rootB] + 1);
}
else
{
t[rootA] = rootB;
h[rootB] = max(h[rootB], h[rootA] + 1);
}
}
int main()
{
int i, op, x, y;
fin >> n >> m;
for (i = 1; i <= n; i++)
t[i] = i;
while (m--)
{
fin >> op >> x >> y;
if (op == 1)
Union(x, y);
else if (op == 2)
{
if (Find(x) == Find(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}