Pagini recente » Cod sursa (job #1220222) | Cod sursa (job #3278260) | Cod sursa (job #3164609) | Cod sursa (job #2177308) | Cod sursa (job #3235628)
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int nmax = 1e5+10;
vector<int> parent(nmax,0),rang(nmax,0);
int n,m;
int find_set(int nod){
if(parent[nod] == 0){
return nod;
}else{
return parent[nod] = find_set(parent[nod]);
}
};
void union_sets(int nod_1,int nod_2){
nod_1 = find_set(nod_1);
nod_2 = find_set(nod_2);
if(nod_1 != nod_2){
if(rang[nod_1] < rang[nod_2]){
swap(nod_1,nod_2);
};
parent[nod_2] = nod_1;
if(rang[nod_1] == rang[nod_2]){
rang[nod_1]++;
}
}
}
int main(){
fin >> n >> m;
int nod_1,nod_2,operatie;
for(int i = 1; i <=m; i++){
fin >> operatie >> nod_1 >> nod_2;
if(operatie == 1){
union_sets(nod_1,nod_2);
}else{
if(find_set(nod_1) == find_set(nod_2)){
fout << "DA" << '\n';
}else{
fout << "NU" << '\n';
}
}
};
return 0;
}