Cod sursa(job #3332934)

Utilizator PatrikKev75Szucs Patrik - Kevin PatrikKev75 Data 9 ianuarie 2026 22:40:03
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
// https://infoarena.ro/problema/disjoint - Szücs Patrik - Kevin
#include <fstream>
#include <vector>

#define st first
#define nd second
#define intp std::pair<int, int>
#define vecp std::vector<intp>

void find(int &x, vecp &set)
{
    while (set[x].nd != x)
        x = set[x].nd;
}

int main()
{
    int n, m;

    std::ifstream in("disjoint.in");
    std::ofstream out("disjoint.out");
    in >> n >> m;

    vecp set(n);
    for (int i = 0; i < n; i++)
    {
        set[i].st = 1;
        set[i].nd = i;
    }

    while (m--)
    {
        int x, y, t;
        in >> t >> x >> y;
        x--;
        y--;

        find(x, set);
        find(y, set);

        if (t == 1)
        {
            if (x != y)
            {
                if (set[x].st < set[y].st)
                    std::swap(x, y);

                set[y].nd = x;
                set[x].st += set[y].st;
            }
        }
        else
        {
            if (set[x].nd == set[y].nd)
                out << "DA\n";
            else
                out << "NU\n";
        }
    }

    out.close();
    return 0;
}