Cod sursa(job #2759248)

Utilizator LucaMihaiLM10Luca Ilie LucaMihaiLM10 Data 16 iunie 2021 12:34:51
Problema Paduri de multimi disjuncte Scor 10
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <stdio.h>

#define MAX_N 100000

int sef[MAX_N];

int find( int x ) {
    if ( sef[x] != x )
        sef[x] = find( sef[x] );
    return sef[x];
}

void onion( int x, int y ) {
    sef[y] = find( x );
}

int main() {
    FILE *fin, *fout;
    int n, q, tip, x, y, i;

    fin = fopen( "disjoint.in", "r" );

    fscanf( fin, "%d%d", &n, &q );
    for ( x = 0; x < n; x++ )
        sef[x] = x;

    fout = fopen( "disjoint.out", "w" );

    for ( i = 0; i < q; i++ ) {
        fscanf( fin, "%d%d%d", &tip, &x, &y );
        if ( tip == 1 )
            onion( x, y );
        else {
            if ( find( x ) == find( y ) )
                fprintf( fout, "DA\n" );
            else
                fprintf( fout, "NU\n" );
        }
    }

    fclose( fin );
    fclose( fout );

    return 0;
}