Cod sursa(job #2375843)

Utilizator LazarRazvanLazar Razvan LazarRazvan Data 8 martie 2019 12:37:08
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;

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

int N, M;
int TT[Nmax], RG[Nmax];
int c,x,y;

int Find(int Nod)
{
    int R, y;
    R = Nod;
    while (TT[Nod] != Nod)   //Cautarea stramosului
    {
        Nod = TT[Nod];
    }

    while (TT[R] != R)       //Compresia arborelui
    {
        y = TT[R];
        TT[R] = Nod;
        R = y;
    }
    return Nod;
}
void Unire(int x, int y)
{
    if (RG[x] > RG[y])
        TT[y] = x;
    else
        TT[x] = y;
    if (RG[x] == RG[y])
        RG[y]++;
}
int main()
{
    fin >> N >> M;
    for (int i=1; i<N; i++)
    {
        TT[i] = i;
        RG[i] = 1;
    }
    for (int i=1; i<=M; i++)
    {
        fin >> c >> x >> y;
        if (c == 2)
        {
            if (Find(x) == Find(y))
                fout << "DA\n";
            else
                fout << "NU\n";
        }
        else
        {
            int tata_x = Find(x);
            int tata_y = Find(y);
            if (tata_x == tata_y)
            {
                // se spune in problema ca tata_x != tata_y
            }
            Unire(tata_x, tata_y);
        }
    }
    return 0;
}