Pagini recente » Istoria paginii utilizator/lotto44 | Cod sursa (job #520565) | Cod sursa (job #1151246) | Cod sursa (job #1097643) | Cod sursa (job #2004150)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,i,o,n1,n2,x,aux,a,y;
struct st
{
int leg, adancime;
}arbore[100010];
int main()
{
f>>n>>m;
//cout<<arbore[5].leg;
for (i=1;i<=m;i++)
{
f>>o>>n1>>n2;
if (o==1)
{
x=n1;
while (arbore[x].leg!=0) {x=arbore[x].leg;}
aux=n1;
while (arbore[aux].leg!=0) {a=aux;aux=arbore[aux].leg;arbore[a].leg=x;}
arbore[x].leg=0;
y=n2;
while (arbore[y].leg!=0) {y=arbore[y].leg;}
aux=n2;
while (arbore[aux].leg!=0) {a=aux;aux=arbore[aux].leg;arbore[a].leg=y;}
arbore[n2].leg=y;
arbore[y].leg=0;
if (arbore[x].adancime>arbore[y].adancime) arbore[y].leg=x;
if (arbore[x].adancime<arbore[y].adancime) arbore[x].leg=y;
if (arbore[x].adancime==arbore[y].adancime) {arbore[x].leg=y;arbore[y].adancime++;}
}
if (o==2)
{
x=n1;
while (arbore[x].leg!=0) {x=arbore[x].leg;}
aux=n1;
while (arbore[aux].leg!=0) {a=aux;aux=arbore[aux].leg;arbore[a].leg=x;}
arbore[x].leg=0;
y=n2;
while (arbore[y].leg!=0) {y=arbore[y].leg;}
aux=n2;
while (arbore[aux].leg!=0) {a=aux;aux=arbore[aux].leg;arbore[a].leg=y;}
arbore[n2].leg=y;
arbore[y].leg=0;
if (y==x) g<<"DA\n";
else g<<"NU\n";
}
}
//cout<<arbore[2].leg;
return 0;
}