Cod sursa(job #3153234)

Utilizator Tudor06MusatTudor Tudor06 Data 28 septembrie 2023 17:54:33
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
//#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
//#include <vector>

//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#define int long long
//#define ll long long

using namespace std;

const int NMAX = 1e5;

int father[NMAX + 1];

int root( int node ) {
    int f, x, aux;
    for ( f = node; father[f] > 0; f = father[f] );
    for ( x = node; father[x] > 0; ) {
        aux = father[x];
        father[x] = f;
        x = aux;
    }
    return f;
}

void unite( int a, int b ) {
    a = root( a );
    b = root( b );
    if ( father[a] > father[b] ) swap( a, b );
    father[a] += father[b] - 1;
    father[b] = a;
}
bool isUnite( int a, int b ) {
    return ( root( a ) == root( b ) );
}


int main() {
    ifstream fin( "disjoint.in" );
    ofstream fout( "disjoint.out" );
    int n, q;
    fin >> n >> q;

    for ( int i = 1, a, b, tip; i <= q; i ++ ) {
        fin >> tip >> a >> b;
        switch( tip ) {
        case 1:
            unite( a, b );
            break;
        default:
            if ( isUnite( a, b ) ) {
                fout << "DA\n";
            } else {
                fout << "NU\n";
            }
        }
    }
    return 0;
}