Cod sursa(job #3126085)

Utilizator PatrunjelxdVarona Antoniu Patrunjelxd Data 5 mai 2023 22:20:38
Problema Heapuri cu reuniune Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.87 kb
#include <iostream>
#include <fstream>

using namespace std;

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

class PairingHeap{
    class Node{
    public:
        int data;
        Node* child;
        Node* next;
        Node(int data){
            this->data = data;
            child = nullptr;
            next = nullptr;
        }
    };
    Node* root;
    public:
    PairingHeap(){
        root = nullptr;
    }
    void insert(int data){
        Node* node = new Node(data);
        if(root == nullptr){
            root = node;
        }else{
            root = merge(root, node);
        }
    }
    Node* merge(Node* node1, Node* node2){
        if(node1 == nullptr){
            return node2;
        }
        if(node2 == nullptr){
            return node1;
        }
        if(node1->data < node2->data){
            Node* temp = node1;
            node1 = node2;
            node2 = temp;
        }
        node2->next = node1->child;
        if(node1->child != nullptr){
            node1->child->next = node2->next;
        }
        node1->child = node2;
        return node1;
    }
    int getMax(){
        return root->data;
    }
    void extractMax(){
        root = merge(root->child, root->child->next);
    }
    PairingHeap merge(PairingHeap heap1, PairingHeap heap2){
        PairingHeap heap;
        heap.root = merge(heap1.root, heap2.root);
        return heap;
    }
};

int main() {
    int n, q;
    cin>>n;
    PairingHeap heap[101];
    cin>>q;
    for(int i=0;i<q;i++){
        int a,b,c;
        cin>>a;
        if(a==1){
            cin>>b>>c;
            heap[b].insert(c);
        }
        else if(a==2){
            cin>>b;
            cout<<heap[b].getMax()<<endl;
            heap[b].extractMax();
        }
        else if(a==3){
            cin>>b>>c;
            heap[b]=heap[b].merge(heap[b],heap[c]);
        }
        }
    return 0;
    }