Pagini recente » Cod sursa (job #149000) | Clasament FMI No Stress 2012 | Cod sursa (job #1227496) | Cod sursa (job #2578904) | Cod sursa (job #3031406)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
const int NMAX = 1e5;
int dim[NMAX + 1], parent[NMAX + 1];
int cauta(int x){
if(x == parent[x])
return x;
return parent[x] = cauta(parent[x]);
}
void unire(int x, int y){
int px = cauta(x);
int py = cauta(y);
if(px == py)
return;
if(dim[px] < dim[py])
swap(px, py);
dim[px] += dim[py];
parent[py] = px;
}
int main(){
int n = 0, q = 0;
cin >> n >> q;
for(int i = 1; i <= n; i++){
parent[i] = i;
dim[i] = 1;
}
for(int i = 0; i < q; i++){
int op = 0, x = 0, y = 0;
cin >> op >> x >> y;
if(op == 1){
unire(x, y);
}else{
if(cauta(x) != cauta(y)){
cout << "NU\n";
}else{
cout << "DA\n";
}
}
}
cin.close();
cout.close();
return 0;
}