Cod sursa(job #1344451)

Utilizator dragos_musanMusan Dragos dragos_musan Data 16 februarie 2015 19:00:42
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>

using namespace std;

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

const int N = 100001;

int t[N];
int n,m;

int radacina(int x)
{
    if (t[x] == 0)
        return x;
    t[x] = radacina(t[x]);
    return t[x];
}

int main()
{
    int cod, x, y;

    f >> n >> m;

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

        if(cod == 1)
        {
            t[x] = radacina(y);
        }
        else if(cod == 2)
        {
            if(radacina(x) == radacina(y))
                g << "DA" << '\n';
            else g << "NU" << '\n';
        }
    }

    return 0;
}