Pagini recente » Cod sursa (job #2326245) | Cod sursa (job #548510) | Cod sursa (job #2413327) | Cod sursa (job #618090) | Cod sursa (job #3229091)
#include <fstream>
#include <vector>
using namespace std;
int radacina(int x, vector <int> &t)
{
if (t[x] == 0)
{
return x;
}
t[x] = radacina(t[x], t);
return t[x];
}
bool verif(int x, int y, vector <int> &t)
{
return (radacina(x, t) == radacina(y, t));
}
void reuniune(int x, int y, vector <int> &t, vector <int> &nr)
{
int rx = radacina(x, t);
int ry = radacina(y, t);
if (nr[rx] < nr[ry])
{
t[rx] = ry;
nr[ry] += nr[rx];
}
else
{
t[ry] = rx;
nr[rx] += nr[ry];
}
}
int main()
{
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n, nr_op;
in >> n >> nr_op;
vector <int> t(n + 1, 0);
vector <int> nr(n + 1, 1);
for (int i = 0; i < nr_op; i++)
{
int tip, x, y;
in >> tip >> x >> y;
if (tip == 1)
{
reuniune(x, y, t, nr);
}
else
{
if (verif(x, y, t))
{
out << "DA\n";
}
else
{
out << "NU\n";
}
}
}
in.close();
out.close();
return 0;
}