Pagini recente » Cod sursa (job #2253789) | Cod sursa (job #1179882) | Cod sursa (job #1773297) | Cod sursa (job #3289029) | Cod sursa (job #2497229)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMAX = 100005;
int t[NMAX];
int h[NMAX];
int findSet(int x)
{
while (x != t[x])
x = t[x];
return x;
}
void uniteSet(int x, int y)
{
x = findSet(x);
y = findSet(y);
if (x == y)
return;
if (h[x] == h[y])
{
t[y] = x;
h[x]++;
}
else if (h[x] < h[y])
t[x] = y;
else
t[y] = x;
}
int main()
{
for (int i = 0; i < NMAX; i++)
{
t[i] = i;
h[i] = 1;
}
int n, q;
in >> n >> q;
while (q--)
{
int t, x, y;
in >> t >> x >> y;
if (t == 1)
uniteSet(x, y);
else
out << (findSet(x) == findSet(y) ? "DA" : "NU") << '\n';
}
}