Pagini recente » Cod sursa (job #2454923) | Cod sursa (job #2279075) | Cod sursa (job #1580177) | Cod sursa (job #3134860)
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[1000000], h[1000000];
int root(int x, int nr) {
if (t[x] == 0) {
return x;
h[x] = nr;
}
root(t[x], nr++);
}
void reuniune(int x, int y) {
int rx = root(x, 1);
int ry = root(y, 1);
if (h[x] < h[y]) {
t[rx] = ry;
}
else{
t[ry] = rx;
if(t[ry] == t[rx])h[rx]++;
}
}
bool verificare(int x, int y) {
return (root(x, 1) == root(y, 1));
}
int main()
{
int n, m;
cin >> n >> m;
int i, x, y;
for (int i = 1; i <= m; i++) {
cin >> i >> x >> y;
if (i == 1) {
reuniune(x, y);
}
else {
if (verificare(x, y) == 1) cout << "DA" << '\n';
else cout << "NU" << '\n';
}
}
}