Pagini recente » Cod sursa (job #2614389) | Cod sursa (job #380378) | Cod sursa (job #2877534) | Cod sursa (job #3238005) | Cod sursa (job #2338021)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX=100000;
int t[NMAX+5], h[NMAX+5];
int FindSet(int x)
{
int cx=x, aux;
while(x!=t[x])
x=t[x];
//Optimizez radacina pt urmatoarele operatii
/* while(cx!=x)
{
aux=t[cx];
t[cx]=x;
cx=aux;
}*/
return x;
}
void UnionSet(int x, int y)
{
//x si y sunt radacinile celor 2 arbori
if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else
{
if(h[x]>h[y])
t[y]=x;
else
t[x]=y;
}
}
int main()
{
int i, n, k, a, b, x, ta, tb;
fin>>n>>k;
for(i=1;i<=n;i++)
{
h[i]=1;
t[i]=i;
}
for(i=1;i<=k;i++)
{
fin>>x>>a>>b;
if(x==1)
{
ta=FindSet(a);
tb=FindSet(b);
UnionSet(ta, tb);
}
else
{
ta=FindSet(a);
tb=FindSet(b);
if(ta==tb)
fout<<"DA"<<"\n";
else
fout<<"NU"<<"\n";
}
}
return 0;
}