Cod sursa(job #2427334)

Utilizator teoschipor00Teodora Schipor teoschipor00 Data 31 mai 2019 16:31:19
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>
#define Nmax 100003

using namespace std;

///------FISIERE-------
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int tata[Nmax], grad[Nmax];

int findFather(int x)
{
    if (tata[x] == x)
        return x;
    else
        return findFather(tata[x]);
}

int main()
{
    int n, m, fx, fy, x, y, cod;
    in >> n >> m;

    for (int i = 1; i <= m; i++)
    {
        tata[i] = i;
        grad[i] = 1;
    }
    for (int i = 1; i <= m; i++)
    {
        in >> cod >> x >> y;
        fx = findFather(x);
        fy = findFather(y);

        if (cod == 1)
        {
            if (grad[fx] < grad[fy])
            {
                tata[fx] = fy;
                grad[fy] += grad[fx];
            }
            else
            {
                tata[fy] = fx;
                grad[fx] += grad[fy];
            }
        }
        else if(cod==2)
        {
            if(findFather(x)==findFather(y))
                out << "DA" << endl;
            else
                out << "NU" << endl;
        }
    }

    return 0;
}