Cod sursa(job #2979856)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 15 februarie 2023 23:57:36
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
/// [A][M][C][B][N] ///
#include <bits/stdc++.h>
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const char sp = ' ', nl = '\n';
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

const int nmax = 1e5;

int p[nmax + 1]{0}, sz[nmax + 1]{0};

int find(int x) {
    return p[x] == x ? x : (p[x] = find(p[x]));
}

void merge(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) {
        return;
    }
    if (sz[y] > sz[x]) {
        swap(x, y);
    }
    p[y] = x;
    sz[x] += sz[y];
}

bool connected(int x, int y) {
    return find(x) == find(y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    fin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        p[i] = i, sz[i] = 1;
    }
    while (m--) {
        int c, x, y;
        fin >> c >> x >> y;
        if (c == 1) {
            merge(x, y);
        }
        else {
            fout << (connected(x, y) ? "DA" : "NU") << nl;
        }
    }

}