Pagini recente » Cod sursa (job #572362) | Cod sursa (job #156167) | Cod sursa (job #2941725) | Cod sursa (job #271416) | Cod sursa (job #2354824)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int N_MAX = 100000;
int n, m, cod, x, y;
int daddy[N_MAX + 2], depth[N_MAX + 2];
int root(int arg) {
while(arg != daddy[arg])
arg = daddy[arg];
return arg;
}
void join(int rootX, int rootY) {
if(depth[rootX] > depth[rootY])
daddy[rootY] = rootX;
if(depth[rootY] > depth[rootX])
daddy[rootX] = rootY;
if(depth[rootX] == depth[rootY]) {
daddy[rootX] = rootY;
depth[rootY]++;
}
}
int main() {
in >> n >> m;
for(int i = 1; i <= n; i++) {
daddy[i] = i;
depth[i] = 1;
}
while(m--) {
in >> cod >> x >> y;
if(cod == 1)
join(root(x), root(y));
else
out << (root(x) == root(y) ? "DA" : "NU") << '\n';
}
return 0;
}