Cod sursa(job #2651282)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 21 septembrie 2020 23:32:26
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#define f in
#define g out

using namespace std;
ifstream in ( "disjoint.in" );
ofstream out( "disjoint.out" );
int n, m, op, x, y, rx, ry, i;
int t[100100];

int rad ( int a ){
    while ( t[a] > 0)
        a = t[a];
    return a;
}

int main() {
    f>>n>>m;
    for ( i=1; i <= n; i++ )
        t[i] = -1;
    for ( ; m--; ){
        f>>op>>x>>y;
        rx = rad(x);
        ry = rad(y);
        if ( op == 1 ){
            if ( t[rx] < t[ry] ){
                t[rx] += t[ry];
                t[ry] = rx;
            } else {
                t[ry] += t[rx];
                t[rx] = ry;
            }
            continue;
        }
        if ( rx == ry )
            g<<"DA\n";
        else g<<"NU\n";
    }
    return 0;
}