Cod sursa(job #3295992)

Utilizator sasha-vovcSasha Vovcenco sasha-vovc Data 10 mai 2025 15:44:42
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>
using namespace std;

int parent[100000];

int main() {
    ifstream input("disjoint.in");
    ofstream output("disjoint.out");
    int N, M;
    input >> N >> M;
    for (int q = 1; q <= N; q++) {
        parent[q] = q;
    }
    for (int q = 0; q < M; q++) {
        int type;
        int x, y;
        input >> type >> x >> y;
        if (type == 1) {
            int parentX = parent[x];
            int parentY = parent[y];
            parent[parentY] = parentX;
        } else {
            bool freq[100000] = {false};
            freq[parent[x]] = true;
            while (x != parent[x]) {
                freq[x] = true;
                x = parent[x];
            }
            bool found = false;
            while (y != parent[y]) {
                if (freq[parent[y]]) {
                    found = true;
                    break;
                }
                y = parent[y];
            }

            cout << (found ? "DA" : "NU") << '\n';
        }
    }
    return 0;
}