Pagini recente » Cod sursa (job #413123) | Cod sursa (job #2804668) | Cod sursa (job #1102179) | Borderou de evaluare (job #982893) | Cod sursa (job #3004035)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 1e5+1;
int n, q, T[NMAX];
int getRoot(int node){
int aux = node;
while(T[node] > 0) node = T[node];
int root = node;
node = aux;
while(node!=root){
aux = T[node];
T[node] = root;
node = aux;
}
return node;
}
void join(int x, int y){
int root_x = getRoot(x);
int root_y = getRoot(y);
if(root_x == root_y) return;
if(T[root_x]<=T[root_y]){
T[root_x] += T[root_y];
T[root_y] = root_x;
}else{
T[root_y] += T[root_x];
T[root_x] = root_y;
}
}
void query(int x, int y){
if(getRoot(x) == getRoot(y)) fout << "DA\n";
else fout << "NU\n";
}
int main(){
fin >> n >> q;
for(int i=1; i<=n; i++) T[i] = -1;
while(q--){
int t, a, b;
fin >> t >> a >> b;
if(t==1){
join(a, b);
}else{
query(a, b);
}
}
}