Pagini recente » Cod sursa (job #1797498) | Cod sursa (job #1804073) | Cod sursa (job #2950760) | Cod sursa (job #2893332) | Cod sursa (job #3001841)
#include <bits/stdc++.h>
using ll=long long;
#define S second
#define F first
#define endl '\n'
#define spid ios_base::sync_with_stdio(false);cin.tie(NULL);
const int mod=1e9+7;
const double pi=3.14159265359;
const int maxn=100001;
using namespace std;
int q,n,parent[100001],depth[100001],c1,c2;
int frep(int i){
int ans;
if(i==parent[i])ans=i;
else{
ans=frep(parent[i]);
}
parent[i]=ans;
return ans;
}
void un(int a,int b){
int arep=frep(a);
int brep=frep(b);
if(arep==brep)return;
else if(depth[arep]>depth[brep]){
parent[brep]=arep;
depth[arep]=max(depth[arep],depth[brep]+1);
}
else{
parent[arep]=brep;
depth[brep]=max(depth[brep],depth[arep]+1);
}
}
int main(){
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
cin>>n>>q;
for(int i=0;i<n;i++){
depth[i]=1;
parent[i]=i;
}
while(q--){
int c;
cin>>c>>c1>>c2;
c1--;c2--;
if(c==1){
un(c1,c2);
}
else{
if(frep(c1)==frep(c2))cout<<"DA"<<endl;
else cout<<"NU"<<endl;
}
}
}