Pagini recente » Monitorul de evaluare | Cod sursa (job #732666) | Cod sursa (job #1569117) | Cod sursa (job #384013) | Cod sursa (job #2920740)
#include <bits/stdc++.h>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
const int dim = 1e5+1;
int t[dim], cnt[dim];
int root (int x)
{
if (t[x] == x)
return x;
t[x] = root(t[x]);
return t[x];
}
void unite (int x, int y)
{
x = root(x);
y = root(y);
if (x != y)
{
if (cnt[x] < cnt[y])
swap(x, y);
}
t[y] = x;
cnt[x] += cnt[y];
}
int main()
{
int n, q;
in >> n >> q;
for (int i=1; i<=n; i++)
t[i] = i, cnt[i] = 1;
while (q--)
{
int op, x, y;
in >> op >> x >> y;
if (op == 1)
unite(x, y);
else
{
if (root(x) == root(y))
out << "DA";
else
out << "NU";
out << '\n';
}
}
return 0;
}