Cod sursa(job #3283051)

Utilizator Edi17roAnghel Eduard Edi17ro Data 7 martie 2025 23:36:29
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#define folosind using
#define numelespatiu namespace
#include <bits/stdc++.h>
///////////////////////////////////////////////////



///get out of my head


folosind numelespatiu std;









///////////////////////////////////////////////////////
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n, m;

vector<int> root, rang;

void init()
{
    for(int i = 1; i <= n; ++i)
    {
        root[i] = i;
        rang[i] = 1;
    }
}

int fnd(int node)
{
    if(root[node] != node)
    {
        root[node] = fnd(root[node]);
    }

    return root[node];
}

void unite(int root1, int root2)
{
    if(root1 != root2)
    {
        if(rang[root1] < rang[root2])
        {
            swap(root1, root2);
        }

        rang[root1] += rang[root2];

        root[root2] = root1;
    }
}

int main()
{
    in >> n >> m;

    root.resize(n + 5);
    rang.resize(n + 5);
    init();

    for(int i = 1; i <= m; ++i)
    {
        int q, x, y;

        in >> q >> x >> y;

        if(q == 1)
        {
            unite(fnd(x), fnd(y));
            continue;
        }

        if(q == 2)
        {
            if(fnd(x) == fnd(y))
            {
                out << "DA" << '\n';
            }
            else
                out << "NU" << '\n';
        }
    }

    return 0;
}