Cod sursa(job #1201404)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 25 iunie 2014 09:31:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.03 kb
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 y
                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";
                //nod1 - radacina arborelui cu nodul x
                //nod2 - radacina arborelui cu nodul y
            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;
}
*/


int tata[Nmax+1];
int h[Nmax+1];

int main()
{
    int i, n, m, x, y, tip, nod1, nod2, nod3;
    fin >> n >> m;
    for(i = 1; i <= n; ++i)
        tata[i] = 0, h[i] = 1;
    for(; m; --m)
    {
        fin >> tip >> x >> y;
        if(tip == 1)
        {
            if(h[x] <= h[y])
            {
                while(tata[x]) x = tata[x];
                    //x - radacina arborelui cu nodul x
                tata[x] = y;
                h[y] += h[x];
            }
            else
            {
                while(tata[y]) y = tata[y];
                    //nod1 - radacina arborelui cu nodul y
                tata[y] = x;
                h[x] += h[y];
            }
        }
        else
        {
            nod1 = x; nod2 = y;
            while(tata[nod1]) nod1 = tata[nod1];
            while(tata[nod2]) nod2 = tata[nod2];
            if(nod1 == nod2) fout << "DA\n";
            else fout << "NU\n";
                //nod1 - radacina arborelui cu nodul x
                //nod2 - radacina arborelui cu nodul y
            if(tata[x]) for(nod3 = tata[x]; nod3 != nod1; )
                tata[x] = nod1, x = nod3, nod3 = tata[nod3];
            if(tata[y]) for(nod3 = tata[y]; nod3 != nod2; )
                tata[y] = nod2, y = nod3, nod3 = tata[nod3];
        }
    }
    return 0;
}