Pagini recente » Cod sursa (job #3003031) | Cod sursa (job #2837723) | Cod sursa (job #1203803) | Cod sursa (job #2641612) | Cod sursa (job #2943659)
//
// Created by Radu Buzas on 21.11.2022.
//
#include <fstream>
#define NMAX 100001
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int tata[NMAX], rang[NMAX];
int N, M;
int root(int k){
if(tata[k]) {
tata[k] = root(tata[k]);
return tata[k];
}
return k;
}
void U(int x, int y){
if(x != y) { // daca nu fac parte din acelasi arbore
if (rang[x] > rang[y])
tata[y] = x;
else
tata[x] = y;
if(rang[x] == rang[y])
rang[y]++;
}
}
int main(){
in >> N >> M;
int op, x, y;
while (M--){
in >> op >> x >> y;
if(op == 1){ // union
U(root(x), root(y));
}
else{ // se verifica apartenenta
if(root(x) == root(y))
out << "DA\n";
else
out << "NU\n";
}
}
in.close();
out.close();
return 0;
}