Pagini recente » Cod sursa (job #3225735) | Cod sursa (job #901560) | Cod sursa (job #1047298) | Cod sursa (job #2545967) | Cod sursa (job #3216367)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n,m,T[100001],R[100001];
int Root(int x)
{
if(T[x]!=x)
return Root(T[x]);
return x;
}
void Reuniune(int x,int y)
{
if(R[x]<R[y])
T[x]=y;
else if(R[x]>R[y])
T[y]=x;
else T[x]=y,R[y]++;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;++i)
T[i]=i;
for(int i=1;i<=m;++i)
{
int op,x,y;
cin>>op>>x>>y;
if(op==1) Reuniune(x,y);
else
{
if(Root(y)==Root(x)) cout<<"DA";
else cout<<"NU";
cout<<'\n';
}
}
return 0;
}