Pagini recente » Cod sursa (job #1820375) | Cod sursa (job #2867298) | Cod sursa (job #515550) | Cod sursa (job #2549515) | Cod sursa (job #3226316)
#include <fstream>
using namespace std;
const int Nmax = 100005;
int father[Nmax], n , m;
int Find_root(int x) {
if(father[x] < 0) {
return x;
}
int root = Find_root(father[x]);
father[x] = root;
return root;
}
void Union(int x, int y) {
int r_x = Find_root(x);
int r_y = Find_root(y);
if(r_x == r_y) {
return;
}
if(father[r_x] < father[r_y]) {
swap(r_x, r_y);
}
father[r_y] += father[r_x];
father[r_x] = r_y;
}
int main() {
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin >> n >> m;
for(int i = 1; i <= n; i++) {
father[i] = -1;
}
while(m--) {
int op, x, y;
fin >> op >> x >> y;
if(op == 1) {
Union(x, y);
}
else {
if(Find_root(x) == Find_root(y)) {
fout << "DA\n";
}
else {
fout << "NU\n";
}
}
}
}