Cod sursa(job #3318199)

Utilizator ilincatudor7Ilinca Tudor ilincatudor7 Data 27 octombrie 2025 14:55:13
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>

using namespace std;

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

int n, m, r[100001], t[100001];

int main()
{
    fin >> n >> m;
    for(int i  =1; i <= n; i++)
    {
        r[i] = 1;
        t[i] = 0;
    }
    for(int i = 1; i <= m; i++)
    {
        int cod, x, y;
        fin >> cod >> x >> y;
        if(cod == 1)
        {
            if(r[x] < r[y]) swap(x, y);
            int p = x, q = y;
            while (t[p])
                p = t[p];
            while(t[q])
                q = t[q];
            t[q] = p;
            r[y] = r[x];
        }
        else
        {
            int p = x, q = y;
            while (t[p])
                p = t[p];
            while(t[q])
                q = t[q];
            if(p == q) fout << "DA\n";
            else fout << "NU\n";
        }
    }
    return 0;
}