Cod sursa(job #3151932)

Utilizator not_anduAndu Scheusan not_andu Data 23 septembrie 2023 10:17:49
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.99 kb
/**
 * Author: Andu Scheusan (not_andu)
 * Created: 23.09.2023 10:12:18
*/

#include <bits/stdc++.h>
#include <unordered_map>
#pragma GCC optimize("O3")

using namespace std;

#define INFILE "disjoint.in"
#define OUTFILE "disjoint.out"

#define all(x) (x).begin(), (x).end()

typedef long long ll;

class DSU {

    private:

        unordered_map<int, int> parent;

    public:

        DSU() {
            // nothing in here
        }

        DSU(int n){

            vector<int> v;

            for(int i = 0; i < n; ++i){

                v.push_back(i);

            }

            makeSet(v);

        }

        void makeSet(vector<int> const&wholeset){

            for(int i : wholeset){
                parent[i] = i;
            }

        }

        int findParent(int val){

            if(parent[val] == val){
                return val;
            }

            return findParent(parent[val]);

        }

        void unionSets(int m, int n){

            int x = findParent(m);
            int y = findParent(n);

            parent[x] = y;

        }

        void print(vector<int> const &universe, DSU &dis){

            for(int i : universe){

                cout << dis.findParent(i) << " ";

            }

            cout << '\n';

        }

};

void solve(){

    DSU dsu;
    int n, m;
    vector<int> v;

    cin >> n >> m;

    for(int i = 1; i <= n; ++i){

        v.push_back(i);

    }

    dsu.makeSet(v);

    for(int i = 0; i < m; ++i){

        short tip;
        int x, y;

        cin >> tip >> x >> y;

        if(tip == 1){

            dsu.unionSets(x, y);

        }
        else{

            if(dsu.findParent(x) == dsu.findParent(y)){

                cout << "DA" << '\n';

            }
            else{

                cout << "NU" << '\n';

            }

        }

    }

}

int main(){
    
    ios_base::sync_with_stdio(false);

    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);

    cin.tie(nullptr);
    cout.tie(nullptr);

    solve();

    return 0;
}