Pagini recente » Cod sursa (job #2792573) | Cod sursa (job #370364) | Cod sursa (job #2527258) | Cod sursa (job #140434) | Cod sursa (job #2750902)
#include <fstream>
using namespace std;
const int N = 100001;
int t[N], nr[N];
int radacina(int x)
{
if (t[x] == 0)
{
return x;
}
//return radacina(t[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 (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, m;
in >> n >> m;
for (int i = 0; i < m; 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;
}