Cod sursa(job #2676538)

Utilizator robeert.77Chirica Robert robeert.77 Data 24 noiembrie 2020 16:04:03
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
vector<int> component[100001];
int n, m, whichComponent[100001];

int main() {
    fin.tie(0);
    fout.tie(0);
    ios::sync_with_stdio(0);
    fin >> n >> m;
    int operation, a, b;
    for (int i = 1; i <= n; i++)
        whichComponent[i] = i;
    for (int i = 0; i < m; i++) {
        fin >> operation >> a >> b;
        if (operation == 1) {
            int maxNode = a, minNode = b;
            if (whichComponent[a] < whichComponent[b])
                swap(minNode, maxNode);
            for (int it : component[whichComponent[maxNode]]) {
                whichComponent[it] = whichComponent[minNode];
                component[whichComponent[minNode]].push_back(it);
            }
            whichComponent[maxNode] = whichComponent[minNode];
            component[whichComponent[minNode]].push_back(maxNode);
        }
        else if (whichComponent[a] == whichComponent[b])
            fout << "DA\n";
        else
            fout << "NU\n";
    }
/*
    for (int i = 1; i <= n; i++)
        cout << i << " -> " << whichComponent[i] << "\n";
    cout << "\n";
    for (int i = 1; i <= n; i++) {
        cout << i << " -> ";
        for (int it : component[i])
            cout << it << " ";
        cout << "\n";
    }*/
    return 0;
}