Cod sursa(job #3295725)

Utilizator STEFANBUSOIStefan Busoi STEFANBUSOI Data 8 mai 2025 00:04:03
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.42 kb
#include <vector>
#include <unordered_map>
#include <fstream>
#include <iostream>


  struct LeftistNode {
        int val;
        LeftistNode *left;
        LeftistNode *right;
        int dist;
      explicit LeftistNode(int _val, LeftistNode *_left=nullptr, LeftistNode *_right=nullptr, int _dist=0):
        val(_val),
        left(_left),
        right(_right),
        dist(_dist) {}
      ~LeftistNode() {
          delete left;
          delete right;
      }
      LeftistNode *clone()
      {
          return new LeftistNode(val, left?left->clone():nullptr,
                                 right?right->clone():nullptr, dist);
      }
  };
  class leftist_heap {
        LeftistNode *root;
      public:
      leftist_heap() {
          root=nullptr;
      };
      LeftistNode * Merge(LeftistNode *node1, LeftistNode *node2) {
          if (node1==nullptr )
              return node2;
          if (node2==nullptr )
              return node1;
          if (node1->val < node2->val) {
              std::swap(node1, node2);
          }
          if (node1->left==nullptr) {
              node1->left=node2;
              return node1;
          }
          node1->right=Merge(node1->right, node2);
          if (node1->left->dist<node1->right->dist) {
              std::swap(node1->left,node1->right);
          }
          node1->dist=node1->right->dist+1;
          return node1;
      }
      void Merge(leftist_heap &rhs)
      {
          if (this == &rhs)
              return;
          root = Merge(root, rhs.root);
          rhs.root = nullptr;
      }

      void insert(int val) {
          root=Merge(new LeftistNode(val),root);
      }
     ~leftist_heap() {
          delete root;
      }
      int findMin()
      {
          return root->val;
      }
      void makeEmpty()
      {
          delete root;
          root = nullptr;
      }
      leftist_heap(leftist_heap &rhs)
      {
          root = nullptr;
          *this = rhs;
      }
      leftist_heap &operator =(const leftist_heap & rhs)
      {
          if (this != &rhs)
          {
              makeEmpty();
              root = rhs.root->clone();
          }
          return *this;
      }
      bool isEmpty()
      {
          return root == nullptr;
      }
      void deleteMin()
      {
          LeftistNode *oldRoot = root;
          root = Merge(root->left, root->right);
          oldRoot->left = nullptr;
          oldRoot->right = nullptr;
          delete oldRoot;
      }
      leftist_heap &operator =(leftist_heap & rhs)
      {
          if (this != &rhs)
          {
              makeEmpty();
              if (!rhs.isEmpty()) {
                  root = rhs.root->clone();
              }
          }
          return *this;
      }

  };


std::ifstream f("../mergeheap.in");
std::ofstream g("../mergeheap.out");
int main() {

    int n,q;
    f>>n>>q;
    std::vector<leftist_heap> heaps(n+3);
    while (q--) {
        int c;
        f>>c;
        if (c==1) {
            int m,x;
            f>>m>>x;
            heaps[m].insert(x);
        }else if (c==2) {
            int m;
            f>>m;
            g<<heaps[m].findMin()<<"\n";
            heaps[m].deleteMin();
        }else {
            int a,b;
            f>>a>>b;
            heaps[a].Merge(heaps[b]);
            heaps[b].makeEmpty();
        }

    }


    return 0;
}