Pagini recente » Cod sursa (job #732439) | Cod sursa (job #1271615) | Cod sursa (job #1920761) | Cod sursa (job #1160461) | Cod sursa (job #2649162)
#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
ifstream fin("disjoint.in"); ofstream fout("disjoint.out");
#define cin fin
#define cout fout
int t, n, m, k, a[300010], q, l, r;
int ptr[100010];
int rnk[100010];
int find_root(int x){
int cr=x;
int h=1;
while(ptr[cr]!=cr){
cr=ptr[cr]; h++;
}
rnk[cr]=h;
return cr;
}
void disjoint_union(int x, int y){
int r1=find_root(x), h1=rnk[r1];
int r2=find_root(y), h2=rnk[r2];
if(h2<h1){
ptr[r2]=r1;
rnk[r1]=max(rnk[r1], rnk[r2]+1);
}
else{
ptr[r1]=r2;
rnk[r2]=max(rnk[r2], rnk[r1]+1);
}
return;
}
bool same_set(int x, int y){
int r1=find_root(x);
int r2=find_root(y);
if(r1==r2){
return true;
}
else{
return false;
}
}
int32_t main(){
INIT
cin>>n>>m;
for(int i=1;i<=n; i++){ptr[i]=i;}
while(m--){
int o, x, y;
cin>>o>>x>>y;
if(o==1){
disjoint_union(x, y);
}
else{
if(same_set(x, y)==true){
cout<<"DA\n";
}
else{
cout<<"NU\n";
}
}
}
return 0;
}