Cod sursa(job #2820252)

Utilizator AswVwsACamburu Luca AswVwsA Data 20 decembrie 2021 09:44:27
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <climits>
#define ll long long
using namespace std;
const int NMAX = 503;
int t[NMAX];

int root(int x)
{
    if (!t[x])
        return x;
    t[x] = root(t[x]);
    return t[x];
}

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

ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int main()
{
    int n, m;
    cin >> n >> m;
    while (m--)
    {
        int op, x, y;
        cin >> op >> x >> y;
        if (op == 1)
            joint(x, y);
        else
            {
                int v1 = root(x);
                int v2 = root(y);
                if (v1 == v2)
                    cout << "DA";
                else
                    cout << "NU";
                cout << "\n";
            }
    }
}