Pagini recente » Cod sursa (job #1700975) | Cod sursa (job #1805324) | Cod sursa (job #2678031) | Cod sursa (job #19358) | Cod sursa (job #3254858)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int MAXN = 1e5;
int root[MAXN + 1];
int height[MAXN + 1];
int N, M;
int getRoot(int node)
{
if (root[node] == node)
return node;
return (root[node] = getRoot(root[node]));
}
void unite(int x, int y)
{
int rootX = getRoot(x), rootY = getRoot(y);
if (height[rootX] < height[rootY])
{
root[rootX] = rootY;
++height[rootX];
}
else
{
root[rootY] = rootX;
}
}
int main()
{
fin >> N >> M;
for (int i = 1; i <= N; i++)
root[i] = i;
int op, a, b;
while (M--)
{
fin >> op >> a >> b;
if (op == 1)
{
unite(a, b);
}
else
{
fout << (getRoot(a) == getRoot(b) ? "DA" : "NU") << '\n';
}
}
return 0;
}