Cod sursa(job #2908772)

Utilizator StanCatalinStanCatalin StanCatalin Data 5 iunie 2022 19:18:14
Problema Heapuri cu reuniune Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.94 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

ifstream in("mergeheap.in");
ofstream out("mergeheap.out");

struct node {
    int value;
    int grad;
    node* father;
    node* child;
    node* next;
    node* previous;
};

const int dim = 105;

deque<node*> vec[dim];

int n, q;

void addChild(node* &A, node* &B) {
    A->grad++;
    if (A->child != nullptr) {
        A->child->previous = B;
    }
    B->next = A->child;
    A->child = B;
    B->father = A;
}

void merge(node* &A, node* &B) {
    if (A->value >= B->value) {
        addChild(A, B);
    } else {
        addChild(B, A);
        A = B;
    }
}

void meld(deque<node*> &vec1, deque<node*> &vec2) {
    if (vec1.empty()) vec1 = vec2;
    else if (!vec2.empty()) {
        node* T1 = vec1[0];
        node* T2 = vec2[0];
        if (T1->grad < T2->grad) {
            vec1.pop_front();
            meld(vec1, vec2);
            vec1.push_front(T1);
        } else if (T1->grad > T2->grad) {
            vec2.pop_front();
            meld(vec1, vec2);
            vec2.push_front(T2);
        } else {
            vec1.pop_front();
            vec2.pop_front();
            merge(T1, T2);

            if (vec1.empty() || vec1[0]->grad > T1->grad) {
                vec1.push_front(T1);
                meld(vec1, vec2);
            } else if (vec2.empty() || vec2[0]->grad > T1->grad) {
                vec2.push_front(T1);
                meld(vec1, vec2);
            } else {
                meld(vec1, vec2);
                vec1.push_front(T1);
            }
        }
    }
}


void insert(deque<node*> &vec, int val) {
    node* aux = new node;
    aux->grad = 0;
    aux->value = val;
    aux->child = nullptr;
    aux->father = nullptr;
    aux->next = nullptr;
    aux->previous = nullptr;
    deque<node*> tmp;
    tmp.push_front(aux);
    meld(vec, tmp);
}

node* maxi(deque<node*> &vec) {
    int maxi = -1;
    node* maxim = nullptr;
    for (auto &y:vec) {
        if (y->value > maxi) {
            maxi = y->value;
            maxim = y;
        }
    }
    return maxim;
}

void deleteMaxi(deque<node*> &vec) {
    node* maxim = maxi(vec);
    int poz = -1;
    for (poz=0; poz<vec.size(); poz++) {
        if (vec[poz] == maxim) break;
    }
    vec.erase(vec.begin() + poz);
    deque<node*> tmp;
    maxim = maxim->child;
    while (maxim != nullptr) {
        maxim->father = nullptr;
        tmp.push_front(maxim);
        maxim = maxim->next;
        if (maxim != nullptr && maxim->previous) {
            maxim->previous->next = nullptr;
        }
    }
    meld(vec, tmp);
}

int main() {
    in >> n >> q;
    int op, x, y;
    while (q--) {
        in >> op >> x;
        if (op == 1) {
            in >> y;
            insert(vec[x], y);
        } else if (op == 2) {
            out << maxi(vec[x])->value << "\n";
            deleteMaxi(vec[x]);
        } else {
            in >> y;
            meld(vec[x], vec[y]);
            vec[y].clear();
        }
    }
    return 0;
}