Pagini recente » Cod sursa (job #1135109) | Cod sursa (job #1630763) | Cod sursa (job #2835989) | Cod sursa (job #1571067) | Cod sursa (job #2736424)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[100001], rang[100001];
int radacina(int x)
{
if(t[x] == 0)
return x;
else return t[x] = radacina(t[x]);
}
void uniune(int x, int y)
{
int rx = radacina(x), ry = radacina(y);
if(rx != ry)
{
if(rang[rx] > rang[ry])
t[ry] = rx;
else
{
t[rx] = ry;
if(rang[rx] == rang[ry])
rang[ry] ++;
}
}
}
inline bool colegi(int x, int y)
{
if(radacina(x) == radacina(y))
return 1;
return 0;
}
int main()
{
int n, m, x, y, op;
in >> n >> m;
/*
for(int i = 1; i <= n; i++)
rang[i] = 1;
*/
for(int i = 1; i <= m; i++)
{
in >> op >> x >> y;
if(op == 1)
uniune(x, y);
else
{
if(colegi(x, y))
out << "DA" << '\n';
else out << "NU" << '\n';
}
}
return 0;
}