Cod sursa(job #2908951)

Utilizator Ioanaand923Ioana Iliescu Ioanaand923 Data 7 iunie 2022 10:53:12
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>

using namespace std;

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

int n, m, v[100005];

int findrep (int x) {
    int tx = x;
    while(tx != v[tx]) {
        tx = v[tx];
    }

    while(x != tx) {
        int nextx = v[x];
        v[x] = tx;
        x = nextx;
    }

    return tx;
}
int main() {

    cin >> n >> m;
    for(int i = 1; i <= n; ++i) {
        v[i] = i;
    }

    for(int i = 1; i <= m; ++i) {
        int op, x, y;
        cin >> op >> x >> y;
        int tx = findrep(x);
        int ty = findrep(y);

        if(op == 1)
            v[tx] = ty; /// sau v[y] = x;

        else {
            if(tx == ty)
                cout << "DA\n";

            else
                cout << "NU\n";
        }
    }

    return 0;
}