Pagini recente » Cod sursa (job #3342774) | Cod sursa (job #3317619) | Monitorul de evaluare | Cod sursa (job #764122) | Cod sursa (job #3317616)
#include <iostream>
#include <fstream>
using namespace std;
int sef[100001],n,m;
int boss(int nod)
{
if(sef[nod] == nod)
return nod;
else
return sef[nod] = boss(sef[nod]);
}
void unite(int x, int y)
{
int sef_x = boss(x);
int sef_y = boss(y);
sef[sef_x] = sef_y;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
sef[i] =i;
for(int i = 1; i <= m; i++)
{
int op,x,y;
cin >> op >> x >> y;
if(op == 1)
unite(x,y);
else
{
x = boss(x);
y = boss(y);
if(sef[x] == sef[y])
cout << "DA\n";
else
cout << "NU\n";
}
}
return 0;
}