Cod sursa(job #2692927)

Utilizator robertnanu_fmiNanu Robert-Ionut robertnanu_fmi Data 4 ianuarie 2021 12:52:00
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include<bits/stdc++.h>

using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

const int nMax = 1e5 + 5;

int n, m, x, y, a, b, v[nMax], rad, cod;

int radacina(int x)
{
    while(v[x]!=0)
        x=v[x];
    return x;
}

int main()
{
    f >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        f >> cod;
        f >> x >> y;

        if(cod == 1)
            v[x] = y;

        if(cod == 2)
        {
            a = radacina(x);
            b = radacina(y);

            if(a == 0 and b == 0)
                g << "NU" << "\n";
            else
            {
                if(a == b)
                    g << "DA" << "\n";
                else
                    g << "NU" << "\n";
            }
        }
    }
    return 0;
}