Pagini recente » Cod sursa (job #1159655) | Cod sursa (job #1374907) | Cod sursa (job #2333558) | Cod sursa (job #1914816) | Cod sursa (job #2194262)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int t[100001];
int radacina(int x)
{
if (t[x] == 0)
return x;
t[x] = radacina(t[x]);
return t[x];
}
void reuniune(int x, int y)
{
int rx = radacina(x), ry = radacina(y);
if (rx == ry)
return;
if (rx < ry)
{
t[rx] = ry;
}
else {
t[ry] = rx;
}
}
bool interogare(int x, int y)
{
return (radacina(x) == radacina(y));
}
int main()
{
int n, p, q, x, y;
f >> n >> p;
for (int i = 1; i <= p; i++)
{
f >> q >> x >> y;
if (q == 2 && interogare(x, y) == true)
g << "DA\n";
if (q == 2 && interogare(x, y) == false)
g << "NU\n";
if (q == 1)
{
reuniune(x, y);
}
}
return 0;
}