Cod sursa(job #3192413)

Utilizator andreidumitrache1709Dumitrache Andrei Bogdan andreidumitrache1709 Data 12 ianuarie 2024 15:57:35
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>
#define MAXN 100000
using namespace std;
int sef[MAXN + 1];
int find_daddy ( int x ) {
    if ( sef[x] == x )
        return x;
    else {
        int p = find_daddy ( sef[x] );
        sef[x] = p;
        return p;
    }
}
int main() {
    ifstream cin ( "disjoint.in" );
    ofstream cout ( "disjoint.out" );
    int n , m , x , y , i , tip;
    cin >> n >> m;
    for ( i = 0 ; i <= n ; i++ )
        sef[i] = i;
    for ( i = 0 ; i < m ; i++ ) {
        cin >> tip >> x >> y;
        if ( tip == 1 )
            sef[find_daddy(x)] = find_daddy( y );
        else if ( tip == 2 )
            if ( find_daddy ( x ) == find_daddy ( y ) )
                cout << "DA\n";
            else
                cout << "NU\n";
    }
    return 0;
}