Pagini recente » Cod sursa (job #3342718) | Cod sursa (job #3341970)
#include <iostream>
#include <fstream>
#define NMAX 100005
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int father[NMAX], n, m;
int find (int x){
while (father[x] != x)
x = father[x];
return x;
}
void unite (int x, int y){
int setX, setY;
setX = find(x);
setY = find(y);
if (setX != setY)
father[y] = x;
}
int main (){
int op, x, y;
in >> n >> m;
for (int i=1; i<=n; ++i)
father[i] = i;
for (int i=1; i<=m; ++i){
in >> op >> x >> y;
if (op == 1)
unite(x, y);
else{
int setX, setY;
setX = find(x);
setY = find(y);
if (setX == setY)
out << "DA";
else out << "NU";
out << '\n';
}
}
return 0;
}