Pagini recente » Cod sursa (job #1284431) | Cod sursa (job #1323956) | Cod sursa (job #1123907) | Cod sursa (job #230280) | Cod sursa (job #3189998)
#include <fstream>
#include <algorithm>
#include <cstring>
#define nmax 100001
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int tata[nmax], h[nmax], c[nmax], n, m, op, x, y;
int rad(int x)
{
if(tata[x]!=0)
return rad(tata[x]);
return x;
}
void unire(int x, int y)
{
int rx = rad(x);
int ry = rad(y);
if(rx==ry)
return;
else
{
if(h[rx]>h[ry]) /// unific arborele mic de cel mare
{
tata[ry]=rx;
}
else
{
tata[rx]=ry;
if(h[rx]==h[ry])
h[ry]++;
}
}
}
int main()
{
cin>>n>>m;
while(m--)
{
cin>>op>>x>>y;
if(op==1)
{
unire(x,y);
}
if(op==2)
{
if(rad(x)==rad(y))
cout<<"DA"<<'\n';
else
cout<<"NU"<<'\n';
}
}
}