Cod sursa(job #2692626)

Utilizator Andrei21AAnea Andrei Andrei21A Data 3 ianuarie 2021 12:52:39
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>
using namespace std;

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

int n, m;
int t[100005];

void Union(int x, int y)
{
    t[x] = y;
}

int Find(int x)
{
    int rad, aux;
    rad = x;
        while (t[rad])
        {
            rad = t[rad];
        }
        while (t[x])
        {
            aux = t[x];
            t[x] = rad;
            x = aux;
        }
        return rad;
    
}

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

        x = Find(x);
        y = Find(y);

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

    }
}

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