Pagini recente » Cod sursa (job #3146771) | Cod sursa (job #1434750) | Cod sursa (job #3004883) | Cod sursa (job #1358042) | Cod sursa (job #2915263)
#include <fstream>
using namespace std;
const int NMAX=100000;
int t[NMAX +5],h[NMAX +5];
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int FindRoot(int x)
{
while(x != t[x])
x=t[x];
return x;
}
void UnionSet(int x,int y) //x si y sunt radacinile a 2 arbori disjuncti
{
if(h[x]>h[y]) //y se leaga de x
t[y]=x;
else // se leaga x de y
if(h[y]>h[x])
t[x]=y;
else // se leaga y de x
{
t[y]=x;
h[x]++;
}
}
int main()
{
int n,m,i,x,y,tx,ty,cod;
fin>>n>>m;
for(i=1;i<=n;++i)
{
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;++i)
{
fin>>cod>>x>>y;
if(cod==1)
{
tx=FindRoot(x);
ty=FindRoot(y);
if(tx!=ty)
UnionSet(tx,ty);
}
if(cod==2)
{
if(FindRoot(x)==FindRoot(y))
fout<<"DA"<<"\n";
else
fout<<"NU"<<"\n";
}
}
return 0;
}