Pagini recente » Cod sursa (job #1906883) | Cod sursa (job #3156660) | Cod sursa (job #2758375) | Cod sursa (job #3226792) | Cod sursa (job #3189979)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n,m,tip,x,y,root[100001],dim[100001];
int getroot(int x){
while(root[x]!=0)
x=root[x];
return x;
}
void join(int x,int y){
int rx=getroot(x),ry=getroot(y);
if(dim[rx]<dim[ry])
swap(rx,ry);
/// rx devine tatal nodului ry
root[ry]=rx;
dim[rx]+=dim[ry];
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
dim[i]=1;
for(int i=1;i<=m;i++){
cin>>tip>>x>>y;
if(tip==1)
join(x,y);
else{
if(getroot(x)==getroot(y))
cout<<"DA";
else
cout<<"NU";
cout<<'\n';
}
}
return 0;
}