Pagini recente » Cod sursa (job #2465993) | Cod sursa (job #1653527) | Cod sursa (job #1128715) | Cod sursa (job #3004479) | Cod sursa (job #1198825)
using namespace std;
#include <fstream>
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int Nmax = 100000;
struct _nod
{
int x;
_nod *tata;
};
_nod* v[Nmax+1];
int h[Nmax+1];
int main()
{
int i, n, m, x, y, tip;
_nod *nod1, *nod2, *nod3;
fin >> n >> m;
for(i = 1; i <= n; ++i)
{
v[i] = new _nod;
v[i]->x = i;
v[i]->tata = NULL;
h[i] = 1;
}
for(; m; --m)
{
fin >> tip >> x >> y;
if(tip == 1)
{
if(h[x] <= h[y])
{
nod1 = v[x];
while(nod1->tata != NULL) nod1 = nod1->tata;
//nod1 - radacina arborelui cu nodul x
nod1->tata = v[y];
h[y] += h[x];
}
else
{
nod1 = v[y];
while(nod1->tata != NULL) nod1 = nod1->tata;
//nod1 - radacina arborelui cu nodul x
nod1->tata = v[x];
h[x] += h[y];
}
}
else
{
nod1 = v[x]; nod2 = v[y];
while(nod1->tata != NULL) nod1 = nod1->tata;
while(nod2->tata != NULL) nod2 = nod2->tata;
if(nod1 == nod2) fout << "DA\n";
else fout << "NU\n";
//for(nod3 = v[x]->tata; nod3 != nod1 && nod3 != NULL; ) v[x]->tata = nod1, v[x] = nod3; nod3 = v[x]->tata;
//for(nod3 = v[y]->tata; nod3 != nod2 && nod3 != NULL; ) v[y]->tata = nod2, v[y] = nod3; nod3 = v[y]->tata;
}
}
return 0;
}