Pagini recente » Cod sursa (job #3214141) | Cod sursa (job #2573809) | Cod sursa (job #914395) | Cod sursa (job #2690177) | Cod sursa (job #634034)
Cod sursa(job #634034)
#include <fstream>
using namespace std;
const int maxn = 100005;
int n,m,tata[maxn],niv[maxn],rx,ry;
int radacina(int x)
{
int aux,c=0,rad;
aux=x;
while(aux!=tata[aux])
{
aux=tata[aux];
c++;
}
rad=aux;
aux=x;
while(aux!=tata[aux])
{
niv[aux]=c;
tata[aux]=rad;
aux=tata[aux];
}
return rad;
}
void unire(int x,int y)
{
if(niv[x]>=niv[y])
tata[ry]=rx;
else
tata[rx]=ry;
}
int main()
{
int x,y,cod;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n>>m;
for(int i=1;i<=n;i++)
tata[i]=i;
for(;m;m--)
{
f>>cod>>x>>y;
rx=radacina(x);
ry=radacina(y);
if(cod==1) unire(x,y);
else if(rx==ry) g<<"DA"<<endl;
else g<<"NU"<<endl;
}
return 0;
}