Cod sursa(job #3179159)

Utilizator Tudor06MusatTudor Tudor06 Data 3 decembrie 2023 10:13:19
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.38 kb
#include <fstream>
#include <vector>
#include <cstring>

const int NMAX = 1e5;

class InParser {
    private:
        std::vector<char> str;
        int ptr;
        std::ifstream fin;

    private:
        char get_char() {
            if (ptr == (int)str.size()) {
                fin.read(str.data(), (int)str.size());
                ptr = 0;
            }

            return str[ptr++];
        }

        template<class T>
        T get_int() {
            char chr;
            do {
                chr = get_char();
            } while (!isdigit(chr) && (chr != '-'));

            int sgn = +1;
            if (chr == '-') {
                sgn = -1;
                chr = get_char();
            }

            T num = 0;
            while (isdigit(chr)) {
                num = num * 10 + (chr - '0');
                chr = get_char();
            }

            return sgn * num;
        }

    public:
        InParser(const char *name) : str((int)1e5), ptr((int)str.size()), fin(name) { }
        ~InParser() { fin.close(); }

        template<class T>
        friend InParser &operator >> (InParser &in, T &num) {
            num = in.get_int<T>();
            return in;
        }
};

class OutParser {
    private:
        std::vector<char> str;
        int ptr;
        std::ofstream fout;

    private:
        void put_char(char chr) {
            if (ptr == (int)str.size()) {
                fout.write(str.data(), (int)str.size());
                ptr = 0;
            }

            str[ptr++] = chr;
        }

        template<class T>
        void put_int(T num) {
            if (num < 0) {
                put_char('-');
                num *= -1;
            }

            if (num > 9)
                put_int(num / 10);
            put_char(num % 10 + '0');
        }

    public:
        OutParser(const char *name) : str((int)1e5), ptr(0), fout(name) { }
        ~OutParser() { fout.write(str.data(), ptr); fout.close(); }

        template<class T>
        friend OutParser &operator << (OutParser &out, const T num) {
            out.put_int(num);
            return out;
        }

        friend OutParser &operator << (OutParser &out, const char chr) {
            out.put_char(chr);
            return out;
        }

        friend OutParser &operator << (OutParser &out, const char *str) {
            for (int i = 0; str[i]; i++)
                out.put_char(str[i]);
            return out;
        }
};

int father[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;
}

int main() {
    InParser fin("disjoint.in");
    OutParser 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:
            a = root( a );
            b = root( b );
            if ( father[a] > father[b] ) std::swap( a, b );
            father[a] += father[b] - 1;
            father[b] = a;
            break;
        default:
            if ( root( a ) == root( b ) ) {
                fout << "DA\n";
            } else {
                fout << "NU\n";
            }
        }
    }

    return 0;
}