Cod sursa(job #2837329)

Utilizator crismariuCrismariu Codrin crismariu Data 22 ianuarie 2022 09:49:03
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("O3")
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define printArr(v, n) for(int i = 0; i < n; i++) cout << v[i] << ' ';
#define readArr(v, n) for(int i = 0; i < n; i++) cin >> v[i];
#define FASTIO cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
#define FILES freopen("disjoint.in", "r", stdin); freopen("disjoint.out", "w", stdout);
using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T,null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define int ll

int d[100001];
int h[100001];

int Find(int node) {
    int root = node;
    while(d[root])
        root = d[root];

    while(d[node]) {
        int aux = d[node];
        d[node] = root;
        node = aux;
    }

    return root;
}

void Union(int x, int y) {
    x = Find(x); y = Find(y);
    if(h[x] == h[y]) {
        h[x]++;

        d[y] = x;
    }
    else if(h[x] > h[y])
        d[y] = x;
    else
        d[x] = y;
}


signed main() {
    FASTIO;
    #ifndef ONLINE_JUDGE
        FILES;
    #endif // ONLINE_JUDGE

    int n, m;
    cin >> n >> m;

    while(m--) {

        int a, b, c;
        cin >> a >> b >> c;

        if(a == 1) {

            Union(b, c);

        } else {

            cout << (Find(b) == Find(c) ? "DA" : "NU") << '\n';

        }

    }

    return 0;
}