Cod sursa(job #2955378)

Utilizator Vincent47David Malutan Vincent47 Data 16 decembrie 2022 21:23:25
Problema Paduri de multimi disjuncte Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>

using namespace std;

    ifstream cin("disjoint.in");
    ofstream cout("disjoint.out");

    int t[505], rang[505];

    int root(int x) {

        int r = x, y;

        while (t[r] != 0)
            r = t[r];

        while (x != r) {

            y = t[x];
            t[x] = r;
            x = y;

        }

        return r;

    }

    void uni(int x, int y) {

        if (rang[x] > rang[y])
            swap(x, y);

        t[x] = y;

        if (rang[x] == rang[y])
            rang[y] ++;

    }


int main()
{

    int n, m, i, c, a, b;
    cin >> n >> m;

    for (i = 1; i <= m; ++i) {

        cin >> c >> a >> b;

            a = root(a);
            b = root(b);

        if (c == 1) {


        if (a != b)
            uni(a, b);

        }

        else {

            if (a == b)
                cout << "DA\n";
            else cout << "NU\n";

        }

    }

    return 0;
}