Pagini recente » Cod sursa (job #31964) | Cod sursa (job #2063600) | ONIS 2014, Clasament | Cod sursa (job #714545) | Cod sursa (job #1242259)
#include <fstream>
#include <cstring>
#define MAXN 100001
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
int s[MAXN], h[MAXN];
int main() {
int op, a, b;
fin >> n >> m;
memset(s, -1, sizeof(s));
memset(h, 1, sizeof(h));
for (int i = 0; i < m; ++i) {
fin >> op >> a >> b;
int rad, aux_a, aux_b, aux;
aux_a = a;
while (s[a] > 0) {
a = s[a];
}
rad = a;
a = aux_a;
while (s[a] > 0) {
aux = a;
a = s[a];
s[aux] = rad;
}
aux_b = b;
while (s[b] > 0) {
b = s[b];
}
rad = b;
while (s[b] > 0) {
aux = b;
b = s[b];
s[aux] = rad;
}
if (op == 1) {
//reuniune
if (h[a] > h[b]) {
s[a] += s[b];
s[b] = a;
h[a] = max(h[a], h[b] + 1);
} else {
s[b] += s[a];
s[a] = b;
h[b] = max(h[a] + 1, h[b]);
}
} else {
if (a == b) {
fout << "DA" << endl;
} else {
fout << "NU" << endl;
}
}
}
return 0;
}