Cod sursa(job #2003723)

Utilizator vlad6001Pintilie Vlad vlad6001 Data 23 iulie 2017 19:01:38
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>
using namespace std;

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

int tine, t[100005], nr, operatii, tip, a, b, t1, t2;

int rad(int x)
{
    if(t[x] == 0)
    return x;
    else
    {
        tine = rad(t[x]);
        t[x] = tine;
        return tine;
    }
}

void modifica(int x, int val)
{
    tine = t[x];
    t[x] = val;
    if(tine == 0)
    return;

    modifica(tine, val);
}

void uneste(int x, int y)
{
    int c = rad(x);
    modifica(y, c);
}

int main()
{
    cin >> nr >> operatii;
    for(int yy=1; yy <= operatii; yy++)
    {
        cin >> tip >> a >> b;
        if(tip == 1)
            uneste(a, b);
        else
        {
            t1 = rad(a);
            t2 = rad(b);
            if(t1 == t2)
            cout << "DA\n";
            else
            cout << "NU\n";
        }
    }
}