Pagini recente » Cod sursa (job #320917) | Cod sursa (job #1501132) | Cod sursa (job #188307) | Cod sursa (job #1868626) | Cod sursa (job #3242768)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
const int N = 1e5;
int father[N + 5], depth[N + 5];
int find_root(int nod){
if(nod == father[nod])
return nod;
return find_root(father[nod]);
}
void unire(int nod1, int nod2){
nod1 = find_root(nod1);
nod2 = find_root(nod2);
if(depth[nod2] < depth[nod1])
father[nod2] = nod1;
else if(depth[nod1] < depth[nod2])
father[nod1] = nod2;
else{
father[nod2] = nod1;
depth[nod1]++;
}
}
int main(){
int n, m;
cin >> n >> m;
for (int i = 0; i <= n; ++i)
father[i] = i;
for(int i = 0; i < m; i++){
int c, x, y;
cin >> c >> x >> y;
if(c == 1)
unire(x, y);
else{
if(find_root(x) != find_root(y))
cout << "NU\n";
else
cout << "DA\n";
}
}
return 0;
}