Pagini recente » Cod sursa (job #2058490) | Cod sursa (job #2861345) | Cod sursa (job #3266790) | Cod sursa (job #1053419) | Cod sursa (job #3230737)
#include <fstream>
using namespace std;
const int N = 100000;
int t[N+1], nrn[N+1];
int radacina(int x)
{
if (t[x] == 0)
{
return x;
}
t[x] = radacina(t[x]);
return t[x];
}
bool verificare(int x, int y)
{
return (radacina(x) == radacina(y));
}
void reuniune(int x, int y)
{
int rx = radacina(x);
int ry = radacina(y);
if (nrn[rx] < nrn[ry])
{
t[rx] = ry;
nrn[ry] += nrn[rx];
}
else
{
t[ry] = rx;
nrn[rx] += nrn[ry];
}
}
int main()
{
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n, q;
in >> n >> q;
///initializarea nu e necesara aici, avand vectori globali
for (int i = 1; i <= n; i++)
{
t[i] = 0;
nrn[i] = 1;
}
for (int i = 0; i < q; i++)
{
int tip, x, y;
in >> tip >> x >> y;
if (tip == 1)
{
reuniune(x, y);
}
else
{
if (verificare(x, y))
{
out << "DA\n";
}
else
{
out << "NU\n";
}
}
}
in.close();
out.close();
return 0;
}