Cod sursa(job #3153242)

Utilizator Tudor06MusatTudor Tudor06 Data 28 septembrie 2023 18:10:10
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.12 kb
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
#include <fstream>
#define magic_sauce inline __attribute__((always_inline))
//#include <iostream>

using namespace std;

const int NMAX = 1e5;

int father[NMAX + 1];
char output[3 * NMAX];

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;
}

const int BUFSIZE = (1024 * 128);

class ReadOnSteroids {
private:
  char buf[BUFSIZE];
  int bp = BUFSIZE - 1;
  int f[128];
  FILE *fin;

public:
  ReadOnSteroids( char *fname ){
    int i;

    for( i = 0 ; i < 128 ; i++ )
      f[i] = 0;
    for( i = '0' ; i <= '9' ; i++ )
      f[i] = 1;

    fin = fopen( fname, "r" );
  }

  magic_sauce char getc(){
    if( !(bp = ((bp + 1) & (BUFSIZE - 1))) )
      fread( buf, sizeof( char ), BUFSIZE, fin );

    return buf[bp];
  }

  magic_sauce int getint(){
    int n = 0, ch;

    while( !f[ch = getc()] );
    do
      n = n * 10 + ch - '0';
    while( f[ch = getc()] );

    return n;
  }
};
int main() {
    ReadOnSteroids fin( (char* )"disjoint.in" );
    FILE* fout = fopen( "disjoint.out", "w" );
    int n, q, o = 0;
    n = fin.getint();
    q = fin.getint();

    for ( int i = 1, a, b, tip; i <= q; i ++ ) {
        tip = fin.getint();
        a = fin.getint();
        b = fin.getint();
//        fin >> tip >> a >> b;
        switch( tip ) {
        case 1:
            a = root( a );
            b = root( b );
            if ( father[a] > father[b] ) swap( a, b );
            father[a] += father[b] - 1;
            father[b] = a;
            break;
        default:
            if ( root( a ) == root( b ) ) {
                output[o++] = 'D';
                output[o++] = 'A';
                output[o++] = '\n';
            } else {
                output[o++] = 'N';
                output[o++] = 'U';
                output[o++] = '\n';
            }
        }
    }
    fwrite( output, o, 1, fout );
    return 0;
}