Cod sursa(job #3251106)

Utilizator vvalentinCiun Valentin vvalentin Data 24 octombrie 2024 22:07:15
Problema Paduri de multimi disjuncte Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int NMAX = 1e5;
int n, m, a, b, c;
int v[NMAX+ 2];
void reuniune(int x, int y) {
    for (int i = 1; i <= n; i++) {
        if (v[i] == x) v[i] = y;
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout); 
    #endif
    
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        v[i] = i;
    }
    while (m--) {
        cin >> a >> b >> c;
        if (a == 1) {
            if (v[b] != v[c]) reuniune(v[b], v[c]);
        }
        else {
            if (v[b] == v[c]) cout << "DA\n";
            else cout << "NU\n";
        }
    }

    return 0;
}