Cod sursa(job #1359837)

Utilizator valentinpielePiele Valentin valentinpiele Data 25 februarie 2015 08:44:08
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>

using namespace std;

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

const int N=100001;
int t[N];
int tip, x, y;
int n, m;

int radacina (int x)
{
    if(t[x] == 0)
        return x;
    t[x]=radacina(t[x]);
    return t[x];
}
void citire ()
{
    f >> n >> m;
    for(int i=1; i<=m; i++)
    {
        f >> tip >> x >> y;
        if(tip == 1)
        {
            x = radacina(x);
            y = radacina(y);
            t[x]=y;
        }
        if(tip == 2)
        {
            if(radacina(x) == radacina(y))
                g << "DA" << '\n';
            else
                g << "NU" << '\n';
        }
    }
}

int main ()
{
    citire ();
    return 0;
}