Cod sursa(job #2939397)

Utilizator lucianul31Moisii Lucian lucianul31 Data 13 noiembrie 2022 17:04:04
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>

using namespace std;

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

const int NMAX = 1e5 + 5;
int Father[NMAX], N, M;

int getFather(int x)
{
    if(Father[x] == 0)
        return x;
    Father[x] = getFather(Father[x]);
    return Father[x];
}

int main()
{
    int x, y, opType, rootX, rootY;
    in >> N >> M;
    for(int i = 1; i <= M; i++)
    {
        in >> opType >> x >> y;
        rootX = getFather(x);
        rootY = getFather(y);
        if(opType == 1)
        {
            if(rootX != rootY)
                Father[rootX] = rootY;
        }
        else
        {
            if(rootX == rootY)
                out << "DA\n";
            else
                out << "NU\n";
        }
    }
    return 0;
}