Pagini recente » Cod sursa (job #1294788) | Cod sursa (job #1354082) | Cod sursa (job #842341) | Cod sursa (job #1451172) | Cod sursa (job #1857091)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int v[100003],rg[100003];
int father(int a)
{
while(v[a])
a=v[a];
return a;
}
int n,i,m,x,y,tx,ty,cx,cod;
int main()
{
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>cod>>x>>y;
if(cod==2)
{
if(father(x)==father(y))
g<<"DA"<<'\n';
else
g<<"NU"<<'\n';
}
else
{
tx=father(x);
ty=father(y);
if(tx!=ty)
{
while(x)
{
cx=x;
x=v[x];
v[x]=tx;
}
if(rg[tx]==rg[ty])
{
rg[tx]+=1;
v[ty]=tx;
}
else
if(rg[tx]>rg[ty])
v[ty]=tx;
else
v[tx]=ty;
}
}
}
return 0;
}