Pagini recente » Monitorul de evaluare | Cod sursa (job #659877) | Cod sursa (job #2544099) | Monitorul de evaluare | Cod sursa (job #3341973)
#include <iostream>
#include <fstream>
#define NMAX 100005
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int father[NMAX], height[NMAX], n, m;
int find (int x){
if (father[x] == x)
return x;
return father[x] = find(father[x]);
}
void unite (int x, int y){
int setX, setY;
setX = find(x);
setY = find(y);
if (setX != setY){
if (height[setX] > height[setY])
father[setY] = setX;
else if (height[setX] < height[setY])
father[setX] = setY;
else{
father[setX] = setY;
++height[setY];
}
}
}
int main (){
int op, x, y;
in >> n >> m;
for (int i=1; i<=n; ++i)
father[i] = i, height[i] = 1;
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;
}