Pagini recente » Cod sursa (job #3365286) | Cod sursa (job #3365804) | Cod sursa (job #3365110) | Cod sursa (job #3365285) | Cod sursa (job #3364664)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int tata[100005],sz[100005];
int rad(int x){
if(tata[x]==x)
return x;
return tata[x]=rad(tata[x]);
}
void unit(int x,int y){
x=rad(x);
y=rad(y);
if(x==y)
return;
if(sz[x]<sz[y])
swap(x,y);
tata[y]=x;
sz[x]+=sz[y];
}
int main() {
int n,m,op,x,y;
cin>>n>>m;
for(int i=1;i<=n;i++){
tata[i]=i;
sz[i]=1;
}
for(int i=0;i<m;i++){
cin>>op>>x>>y;
if(op==1)
unit(x,y);
else{
if(rad(x)==rad(y))
cout<<"DA";
else
cout<<"NU";
cout<<"\n";
}
}
return 0;
}