Pagini recente » Cod sursa (job #1836927) | Cod sursa (job #2870216) | Cod sursa (job #140506) | Cod sursa (job #2723163) | Cod sursa (job #2938062)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
int h[100005], t[100005];
int radacina (int vf)
{
if (t[vf] == vf)
return vf;
return radacina(t[vf]);
}
void reunesc (int x, int y)
{
if (h[x] > h[y])
t[y] = x;
else if (h[y] > h[x])
t[x] = y;
else
{
t[x] = y;
h[x]++;
}
}
int main()
{int n, m, i, q, x, y;
f >> n >> m;
for (i = 1; i <= n; i++)
{
h[i] = 1;
t[i] = i;
}
for (i = 1; i <= m; i++)
{
f >> q >> x >> y;
if (q == 1)
reunesc(radacina(x), radacina(y));
else if (radacina(x) == radacina(y))
g << "DA\n";
else g << "NU\n";
}
f.close();
g.close();
return 0;
}