Cod sursa(job #3215169)

Utilizator andreipirjol5Andrei Pirjol andreipirjol5 Data 14 martie 2024 18:33:33
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>

using namespace std;

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

const int NMAX = 1e5;
int father[NMAX + 5];
int root(int node)
{
    if(father[node] == node)
        return node;

    return node = root(father[node]);
}

void Union(int node1, int node2)
{
    father[root(node1)] = father[root(node2)];
}

int main()
{
    int n, m;
    fin >> n >> m;

    for(int i = 1; i <= n; i++)
        father[i] = i;

    for(int i = 1; i <= m; i++)
    {
        int op, x, y;
        fin >> op >> x >> y;

        if(op == 1)
            Union(x, y);
        else if(op == 2)
        {
            if(root(x) == root(y))
                fout << "DA\n";
            else fout << "NU\n";
        }
    }

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