Cod sursa(job #1548239)

Utilizator ArambasaVlad Arambasa Arambasa Data 10 decembrie 2015 17:53:33
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

const int NMax = 100005;

int N,M;
int T[NMax],R[NMax];

int Father(int Nod)
{
    int Rad = Nod;
    while(Rad!=T[Rad])
        Rad = T[Rad];

    while(Nod!=T[Nod])
        {
            int Aux = T[Nod];
            T[Nod] = Rad;
            Nod = Aux;
        }

    return Rad;
}

void Unite(int x, int y)
{
    /*if(R[x] > R[y])
       T[y] = x;
    if(R[y] > R[x])
        T[x] = y;
    if(R[x] == R[y])
        {
            T[x] = y;
            R[y]++;
        }*/
    T[x] = y;

}

int main()
{
    fin>>N>>M;
    for(int i = 1; i<=N; i++)
        T[i] = i;
    while(M--)
    {
        int op,x,y;
        fin>>op>>x>>y;
        if(op == 1)
            Unite(Father(x),Father(y));
        if(op == 2)
            if(Father(x)==Father(y))
                fout<<"DA\n";
            else
                fout<<"NU\n";
    }
    return 0;
}