Pagini recente » Cod sursa (job #1298204) | Cod sursa (job #2090648) | Cod sursa (job #1775654) | Cod sursa (job #2772158) | Cod sursa (job #459637)
Cod sursa(job #459637)
#include<fstream>
using namespace std;
int n, m;
int t[100001], h[100001];
void init()
{
for (int i = 1; i <= n; ++i)
{
t[i] = i;
h[i] = 0;
}
}
void unify(int x, int y)
{
if (h[x] > h[y])
t[y] = x;
if (h[y] > h[x])
t[x] = y;
if (h[x] == h[y])
{
t[y] = x;
++h[x];
}
}
int find(int x)
{
if (t[x] != x)
t[x] = find(t[x]);
return t[x];
}
int main()
{
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin >> n >> m;
init();
int op, p1, p2;
for (int i = 1; i <= m; ++i)
{
fin >> op >> p1 >> p2;
switch (op)
{
case 1:
unify(find(p1), find(p2));
break;
case 2:
fout << (find(p1) == find(p2) ? "DA" : "NU") << '\n';
break;
}
}
}