Pagini recente » Profil Madalina_Nic | Cod sursa (job #499831) | Cod sursa (job #1395963) | Cod sursa (job #1208159) | Cod sursa (job #2968014)
#include <fstream>
using namespace std;
ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
int n, m, tata[100010], h[100010];
int gaseste (int nod)
{
if (nod != tata[nod])
tata[nod] = gaseste(tata[nod]);
return tata[nod];
}
void unire(int nod1, int nod2) {
int tata1 = gaseste(nod1);
int tata2 = gaseste(nod2);
if (h[tata1] > h[tata2]) {
tata[tata2] = tata1;
} else if (h[tata1] < h[tata2]) {
tata[tata1] = tata2;
} else {
tata[tata1] = tata2;
h[tata2]++;
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
tata[i] = i, h[i] = 1;
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;
}