Pagini recente » infoarena - comunitate informatica, concursuri de programare | infoarena - te ajutam sa devii olimpic! | Cod sursa (job #3181574) | infoarena - te ajutam sa devii olimpic! | Cod sursa (job #2425555)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int tata[100009], grad[100009];
int find_father(int x)
{
if (x == tata[x])
return x;
return find_father(tata[x]);
}
int main()
{
int n, m, tip_operatie, x, y;
fin >> n >> m;
for (int i = 0; i < n; i++)
{
tata[i] = i;
grad[i] = 1;
}
for (int i = 0; i < m; i++)
{
fin >> tip_operatie;
fin >> x >> y;
int fx = find_father(x);
int fy = find_father(y);
if (tip_operatie == 1)
{
if (grad[fx] < grad[fy])
{
tata[fx] = fy;
grad[fy] += grad[fx];
}
else
{
tata[fy] = fx;
grad[fx] += grad[fy];
}
}
if (tip_operatie == 2)
{
if (fx == fy)
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}