Cod sursa(job #2951864)

Utilizator C_R_I_S_T_I_2_3Cristi Gavrila C_R_I_S_T_I_2_3 Data 7 decembrie 2022 17:59:24
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, x, y, op;
int t[100005];
void unire(int x, int y)
{
    t[y] = x;
}
int radacina(int x)
{
    int aux;
    int r = x;
    while(t[r])
    {
        r = t[r];
    }
    while(t[x])
    {
        aux = t[x];
        t[x] = r;
        x = aux;
    }
    return r;
}
int main()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i ++)
    {
        fin >> op >> x >> y;
        x = radacina(x);
        y = radacina(y);
        if(op == 1)
        {
            unire(x, y);
        }
        if(op == 2)
        {
            if(x == y)
                fout << "DA";
            else
                fout << "NU";
            fout << "\n";
        }
    }
    return 0;
}