Pagini recente » Cod sursa (job #1820495) | Cod sursa (job #2539575) | Cod sursa (job #2540010) | Cod sursa (job #2533894) | Cod sursa (job #2967998)
#include <fstream>
using namespace std;
ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
int n, m, tata[100002];
int gaseste (int nod)
{
if (nod == tata[nod])
return nod;
tata[nod] = gaseste(tata[nod]);
}
void unire(int nod1, int nod2) {
int tata1 = gaseste(nod1);
int tata2 = gaseste(nod2);
tata[tata1] = tata2;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
tata[i] = i;
for (int i = 1; i <= m; i++)
{
int tip, x, y;
cin >> tip >> x >> y;
if (tip == 1) {
unire(x, y);
}
else {
if (gaseste(x) == gaseste(y)) cout << "DA" << endl;
else cout << "NU" << endl;
}
}
return 0;
}