Pagini recente » Cod sursa (job #897703) | Cod sursa (job #465542) | Borderou de evaluare (job #702908) | Cod sursa (job #1052877) | Cod sursa (job #2755922)
#include<iostream>
#include<fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int mx=1e5+100;
int n,m,father[mx],depth[mx];
int root(int node){
while(father[node]){
node=father[node];
}
return node;
}
void merge(int a,int b){
if(depth[a]==depth[b]){
father[a]=b;
depth[b]++;
}
else if(depth[a]<depth[b]){
father[a]=b;
}
else{
father[b]=a;
}
}
void read(){
in>>n>>m;
}
void solve(){
int x,a,b;
for(int i=0;i<m;i++){
in>>x>>a>>b;
if(x==1){
merge(root(a),root(b));
}
else{
root(a)==root(b)?out<<"DA\n":out<<"NU\n";
}
}
}
int main(){
read();
solve();
return 0;
}