Pagini recente » Cod sursa (job #2866214) | Cod sursa (job #580624) | Cod sursa (job #1222183) | Cod sursa (job #1421437) | Cod sursa (job #2910468)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int nMAX = 100e3;
int n, m;
int tata[nMAX + 1];
int radacina(int nod)
{
if (tata[nod])
return radacina(tata[nod]);
return nod;
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; ++i)
tata[i] = 0;
for (int i = 1; i <= m; ++i)
{
int op, x, y;
fin >> op >> x >> y;
if (op == 1)
{
// mutam tot arborele din care face parte y intr-un fiu al radacinii arborelui in care face parte x.
// de asemenea, setam tatii nodurilor prin care trecem in drumul y..rady la x
int radx = radacina(x);
int nod = y;
while (nod != 0)
{
int tatanod = tata[nod];
tata[nod] = radx;
nod = tatanod;
}
}
else if (op == 2)
{
fout << (radacina(x) == radacina(y) ? "DA\n" : "NU\n");
}
}
}