Cod sursa(job #1746463)

Utilizator BogauuuBogdan Ivancu Bogauuu Data 23 august 2016 13:28:46
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>

using namespace std;

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

int t[100002];

int rad(int x)
{
    while (t[x]>0) x=t[x];

    return x;
}

int n,m,i,x,y,rx,ry,q;

int main()
{
    fin >> n >> m;
    for (i=1;i<=n;i++) t[i]=-1;
    for (i=1;i<=m;i++)
    {
        fin >> q >> x >> y;
        rx=rad(x);
        ry=rad(y);
        if (q==2)
        {
            if (rx==ry) fout << "DA" << "\n";
            else fout << "NU" << "\n";
        }
        else
        {
            if (rx!=ry)
            {
                if(t[rx]<t[ry])
                {
                    t[rx]+=t[ry];
                    t[ry]=rx;
                }
                else
                {
                    t[ry]+=t[rx];
                    t[rx]=ry;
                }
            }

        }
    }

    return 0;
}