Pagini recente » Cod sursa (job #1478764) | Cod sursa (job #2829441) | Cod sursa (job #1485396) | Cod sursa (job #1384352) | Cod sursa (job #1366682)
#include <fstream>
using namespace std;
const int kMaxN = 100005;
ifstream in;
ofstream out;
int father[kMaxN], deep[kMaxN];
void unite(int a, int b) {
if (deep[a] > deep[b]) {
father[b] = a;
} else {
deep[b] = max(deep[b], deep[a] + 1);
father[a] = b;
}
}
int getFather(int nod) {
// fara compresie
if (father[nod] == nod)
return nod;
return getFather(father[nod]);
}
int main() {
in.open("disjoint.in");
int n, m; in >> n >> m;
for (int i = 1; i <= n; ++i) {
father[i] = i;
deep[i] = i;
}
out.open("disjoint.out");
while (m--) {
int t; in >> t;
if (t == 1) {
int x, y; in >> x >> y;
unite(getFather(x), getFather(y));
} else {
int x, y; in >> x >> y;
out << ((getFather(x) == getFather(y)) ? ("DA") : ("NU")) << '\n';
}
}
in.close();
out.close();
return 0;
}