Advertisement
smatskevich

Seminar9

Apr 10th, 2023
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <set>
  4. #include <unordered_map>
  5. #include <vector>
  6.  
  7. struct PriorityQueue {
  8.  public:
  9.   void Push(int v, int distance);
  10.   // Возвращает индекс вершины
  11.   int Pop();
  12.   void DecreaseKey(int v, int old_distance, int distance);
  13.  
  14.   void Print() const { for (auto [d, v] : distances_and_vertices) std::cout << d << " " << v << ", "; std::cout << std::endl; }
  15.  
  16.  private:
  17.   std::set<std::pair<int, int>> distances_and_vertices;
  18. };
  19.  
  20. void PriorityQueue::Push(int v, int distance) {
  21.   distances_and_vertices.insert({distance, v});
  22. }
  23. int PriorityQueue::Pop() {
  24.   auto it = distances_and_vertices.begin();
  25.   int result = it->second;
  26.   distances_and_vertices.erase(it);
  27.   return result;
  28. }
  29. void PriorityQueue::DecreaseKey(int v, int old_distance, int distance) {
  30.   distances_and_vertices.erase({old_distance, v});
  31.   distances_and_vertices.insert({distance, v});
  32. }
  33.  
  34. int main1() {
  35.   PriorityQueue pq;
  36.   pq.Push(4, 9);
  37.   pq.Push(1, 8);
  38.   pq.Print();
  39.   pq.DecreaseKey(4, 9, 7);
  40.   pq.Print();
  41.   return 0;
  42. }
  43.  
  44. struct PriorityQueue2 {
  45.  public:
  46.   void Push(int v, int distance);
  47.   // Возвращает индекс вершины
  48.   int Pop();
  49.   void DecreaseKey(int v, int distance);
  50.  
  51.   void Print() const { for (auto [d, v] : heap) std::cout << d << " " << v << ", "; std::cout << std::endl; }
  52.  
  53.  private:
  54.   std::vector<std::pair<int, int>> heap;
  55.   //std::vector<size_t> v2head_index;  Нужно знать, сколько вершин в такой версии
  56.   std::unordered_map<int, size_t> v2index;
  57.  
  58.   void SiftUp(size_t pos);
  59.   void SiftDown(size_t pos);
  60. };
  61.  
  62. void PriorityQueue2::Push(int v, int distance) {
  63.   heap.push_back({distance, v});
  64.   v2index[v] = heap.size() - 1;
  65.   SiftUp(heap.size() - 1);
  66. }
  67.  
  68. int PriorityQueue2::Pop() {
  69.   assert(!heap.empty());
  70.   int result = heap.front().second;
  71.   v2index.erase(result);
  72.   heap[0] = heap.back();
  73.   heap.pop_back();
  74.   if (heap.empty()) return result;
  75.   v2index[heap[0].second] = 0;
  76.  
  77.   SiftDown(0);
  78.   return result;
  79. }
  80.  
  81. void PriorityQueue2::DecreaseKey(int v, int distance) {
  82.   size_t index = v2index[v];
  83.   heap[index].first = distance;
  84.   SiftUp(index);
  85. }
  86.  
  87. void PriorityQueue2::SiftUp(size_t pos) {
  88.   while (pos > 0) {
  89.     size_t parent = (pos - 1) / 2;
  90.     if (heap[parent] <= heap[pos]) break;
  91.     v2index[heap[pos].second] = parent;
  92.     v2index[heap[parent].second] = pos;
  93.     std::swap(heap[parent], heap[pos]);
  94.     pos = parent;
  95.   }
  96. }
  97.  
  98. void PriorityQueue2::SiftDown(size_t pos) {
  99.   while (true) {
  100.     size_t minimum = pos;
  101.     size_t left = pos * 2 + 1;
  102.     if (left < heap.size() && heap[left] < heap[minimum]) minimum = left;
  103.     size_t right = pos * 2 + 2;
  104.     if (right < heap.size() && heap[right] < heap[minimum]) minimum = right;
  105.     if (minimum == pos) return;
  106.     v2index[heap[pos].second] = minimum;
  107.     v2index[heap[minimum].second] = pos;
  108.     std::swap(heap[pos], heap[minimum]);
  109.   }
  110. }
  111.  
  112. int main3() {
  113.   PriorityQueue2 pq;
  114.   pq.Push(4, 9);
  115.   pq.Push(1, 8);
  116.   pq.Print();
  117.   pq.DecreaseKey(4, 7);
  118.   pq.Print();
  119.   pq.Pop();
  120.   pq.Print();
  121.   return 0;
  122. }
  123.  
  124. //class MyPair {
  125. // public:
  126. //  MyPair& operator=(MyPair&& from) {
  127. //    long long shift = this - &from;
  128. //    q.v2index[from] += shift;
  129. //  }
  130. //};
  131. //int main2() {
  132. //  //
  133. //  priority_queue<MyPair>
  134. //  return 0;
  135. //}
  136.  
  137. class DSU {
  138.  public:
  139.   explicit DSU(int n);
  140.   int Find(int v);
  141.   void Union(int u, int v);
  142.   void Print() const;
  143.  
  144.  private:
  145.   std::vector<int> parents;
  146. };
  147.  
  148. DSU::DSU(int n) : parents(n) {
  149.   for (int i = 0; i < n; ++i) parents[i] = i;
  150. }
  151. int DSU::Find(int v) {
  152.   while (v != parents[v]) v = parents[v];
  153.   return v;
  154. }
  155. void DSU::Union(int u, int v) {
  156.   u = Find(u);
  157.   v = Find(v);
  158.   parents[v] = u;
  159. }
  160. void DSU::Print() const {
  161.   for (int i = 0; i < parents.size(); ++i) {
  162.     int u = i;
  163.     for (; u != parents[u]; u = parents[u]) std::cout << u << " -> ";
  164.     std::cout << u << "\n";
  165.   }
  166. }
  167.  
  168. int main() {
  169.   DSU dsu(10);
  170.   dsu.Union(8, 0);
  171.   dsu.Union(5, 0);
  172.   dsu.Union(8, 2);
  173.   dsu.Union(7, 5);
  174.   dsu.Union(3, 6);
  175.   dsu.Union(3, 9);
  176.   dsu.Union(3, 4);
  177.   dsu.Print();
  178.  
  179.   return 0;
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement