Cod sursa(job #3271471)

Utilizator Alex_AeleneiAlex Aelenei Ioan Alex_Aelenei Data 26 ianuarie 2025 12:27:10
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <numeric>
#include <fstream>
#include <vector>

using namespace std;

class DisjointSets
{
private:
    vector<int> parent;
    vector<int> height;

public:
    DisjointSets(int nodeCount)
    {
        parent = vector<int>(nodeCount + 1);
        height = vector<int>(nodeCount + 1, 1);
        iota(parent.begin(), parent.end(), 0);
    }

    int getParent(int node)
    {
        while (parent[node] != node)
            node = parent[node];
        return node;
    }

    bool areInSameSet(int firstNode, int secondNode)
    {
        return getParent(firstNode) == getParent(secondNode);
    }

    void unite(int firstNode, int secondNode)
    {
        if (height[firstNode] <= height[secondNode])
        {
            parent[firstNode] = secondNode;
            height[secondNode]++;
        }
        else
        {
            parent[secondNode] = firstNode;
            height[firstNode]++;
        }
    }
};

int main()
{
    ifstream in("disjoint.in");
    ofstream out("disjoint.out");

    int nodeCount, operationsCount, type, source, destination;
    in >> nodeCount >> operationsCount;

    DisjointSets disjointSets(nodeCount);
    for (int operationIndex = 1; operationIndex <= operationsCount; operationIndex++)
    {
        in >> type >> source >> destination;
        if (type == 1)
            disjointSets.unite(source, destination);
        else if (type == 2)
            out << (disjointSets.areInSameSet(source, destination) ? "DA" : "NU") << '\n';
    }
    return 0;
}