Pagini recente » Cod sursa (job #2703585) | Cod sursa (job #522246) | Cod sursa (job #2646757) | Cod sursa (job #1556726) | Cod sursa (job #3192450)
#include <fstream>
#include <algorithm>
#include <queue>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int t[100005],i,n,m,c,x,y;
int rad(int x)
{
int k1=x;
while(t[x]>0)
x=t[x];
int k2=x;
x=k1;
while(x!=k2)
{
k1=t[x];
t[x]=k2;
x=k1;
}
return k2;
}
bool ququ(int x,int y)
{
return rad(x)==rad(y);
}
void join(int x,int y)
{
int k1=rad(x),k2=rad(y);
if(k1==k2)
return;
if(t[k1]<=t[k2])
{
t[k1]+=t[k2];
t[k2]=k1;
}
else
{
t[k2]+=t[k1];
t[k1]=k2;
}
}
int main()
{
cin>>n>>m;
for(i=1;i<=n;i++)
t[i]=-1;
for(i=1;i<=m;i++)
{
cin>>c>>x>>y;
if(c==1)
join(x,y);
else
{
if(ququ(x,y)==true)
cout<<"DA"<<"\n";
else
cout<<"NU"<<"\n";
}
}
return 0;
}