Cod sursa(job #2824216)

Utilizator Avram_RobertAvram Robert Ionut Avram_Robert Data 31 decembrie 2021 14:56:27
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <climits>
#include <string>
#include <algorithm>
#include <fstream>

using namespace std;

const int NMAX = 100000;

int tata[1 + NMAX];

int main()
{
    ifstream in("disjoint.in");
    ofstream out("disjoint.out");
    int n, m;
    in >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        tata[i] = i;
    }
    for (int i = 1; i <= m; i++)
    {
        int tipOperatie, a, b;
        in >> tipOperatie >> a >> b;
        if (tipOperatie == 2)
        {
            if (tata[a] == tata[b])
            {
                out << "DA" <<'\n';
            }
            else
                out << "NU" << '\n';
        }
        else
        {
            int c = tata[b];
            for (int j = 1; j <= n; j++)
            {
                if (tata[j] == c)
                {
                    tata[j] = tata[a];
                }
            }
        }
    }
}