Cod sursa(job #2752346)

Utilizator pokermon2005paun darius alexandru pokermon2005 Data 17 mai 2021 18:50:49
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>

using namespace std;

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

int t[100001], rang[100001];

int radacina(int x)
{
    if(t[x] == 0)
        return x;
    else return t[x] = radacina(t[x]);
}
void uniune(int x, int y)
{
    int rx = radacina(x), ry = radacina(y);
    if(rx != ry)
    {
        if(rang[rx] > rang[ry])
            t[ry] = rx;
        else
        {
            t[rx] = ry;
            if(rang[rx] == rang[ry])
                rang[ry] ++;
        }
    }
}
inline bool colegi(int x, int y)
{
    if(radacina(x) == radacina(y))
        return 1;
    return 0;
}
int main()
{
    int n, m, x, y, op;
    in >> n >> m;
    /*
    for(int i = 1; i <= n; i++)
        rang[i] = 1;
    */
    for(int i = 1; i <= m; i++)
    {
        in >> op >> x >> y;
        if(op == 1)
            uniune(x, y);
        else
        {
            if(colegi(x, y))
                out << "DA" << '\n';
            else out << "NU" << '\n';
        }
    }
    return 0;
}