Pagini recente » Cod sursa (job #1838726) | Cod sursa (job #45687) | Cod sursa (job #927478) | Cod sursa (job #2812662) | Cod sursa (job #2559465)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[100001], nr[100001];
int radacina(int x)
{
if(t[x] == 0)
return x;
t[x] = radacina(t[x]);
return t[x];
}
void reunire(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];
}
}
bool interogare(int x, int y)
{
return (radacina(x) == radacina(y));
}
int main()
{
int n, m, x, y, c;
in >> n >> m;
for(int i = 1; i <= m; i++)
{
in >> c >> x >> y;
if(c == 1)
{
reunire(x, y);
}
else if(c == 2)
{
if(interogare(x, y) == 1)
out << "DA" << "\n";
else out << "NU" << "\n";
}
}
return 0;
}