Pagini recente » Cod sursa (job #2194024) | Cod sursa (job #245375) | Cod sursa (job #1396030) | Cod sursa (job #318869) | Cod sursa (job #3189976)
#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(x,y);
/// x devine tatal nodului y
root[y]=x;
dim[x]+=dim[y];
}
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;
}