Cod sursa(job #2023867)

Utilizator FredyLup Lucia Fredy Data 19 septembrie 2017 16:52:01
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define lim 100010
int d[lim];


int get_dad(int x)
{
    if(x != d[x]) d[x] = get_dad(d[x]);
    return d[x];
}


int main()
{
    int n, m, x, y, c, aux1, aux2;
    fin >> n >> m;
    for (int i=1; i <= n; i++)
        d[i] = i;
    for (int i=1; i <= m; i++)
    {
        fin >> c >> x >> y;
        if (c == 1)
            d[y] = get_dad(x);
        else
        {
            aux1 = get_dad(x);
            aux2 = get_dad(y);
            if (aux1 == aux2)   fout << "DA\n";
            else    fout << "NU\n";
        }
    }

    fin.close();
    fout.close();
    return 0;
}