Cod sursa(job #3317624)

Utilizator hiAvidMihaly David-Gabriel hiAvid Data 24 octombrie 2025 17:42:04
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>

using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");

int sef[100001];
int s(int x)
{
    if (sef[x] == x)
        return x;
    else
        return sef[x] = s(sef[x]);
}

void u(int x, int y)
{
    int sefx = s(x), sefy = s(y);
    sef[sefy] = sefx;
}

int main()
{
    int n, m, i, cod, x, y, sefx, sefy;
    in >> n >> m;
    for (i=1;i<=n;i++)
        sef[i] = i;
    for (i=1;i<=m;i++)
    {
        in >> cod >> x >> y;
        if (cod == 1 )
        {
            sefx = s(x);
            sefy = (y);
            if (sefx != sefy)
                u(x, y);
        }
        else
        {
            sefx = s(x);
            sefy = s(y);
            if (sefx == sefy)
                out << "DA" << "\n";
            else
                out << "NU" << "\n";
        }
    }
    return 0;
}