Advertisement
den4ik2003

Untitled

Sep 5th, 2023
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using std::vector;
  6.  
  7. void DFS(int v, vector<vector<int>>& neighbours, vector<bool>& used, int num_of_component) {
  8.   used[v] = true;
  9.   for (int to : neighbours[v]) {
  10.     if (used[to]) {
  11.       continue;
  12.     }
  13.     DFS(to, neighbours, used, num_of_component);
  14.   }
  15. }
  16.  
  17. int NumOfComponents(vector<vector<int>>& neighbours, int n) {
  18.   vector<bool> used(n, false);
  19.   int num_of_component = 1;
  20.   for (int i = 0; i < n; ++i) {
  21.     if (!used[i]) {
  22.       std::vector<std::vector<int>> currentNeighbours;
  23.       DFS(i, currentNeighbours, used, num_of_component++);
  24.     }
  25.   }
  26.   return num_of_component;
  27. }
  28.  
  29. vector<vector<int>> SelectAppropriateGraph(vector<vector<std::pair<int, int>>>& neighboursWithWeight, int x, int n) {
  30.   vector<vector<int>> appropriateGraph(n);
  31.   for (int i = 0; i < neighboursWithWeight.size(); ++i) {
  32.     for (int j = 0; j < neighboursWithWeight[i].size(); ++j) {
  33.       if (neighboursWithWeight[i][j].second > x) {
  34.         appropriateGraph[i].push_back(j);
  35.       }
  36.     }
  37.   }
  38.   return std::move(appropriateGraph);
  39. }
  40.  
  41. int BinaryFindAnswer(vector<vector<std::pair<int, int>>>& neighboursWithWeight, int max, int initialNum, int n) {
  42.   int left = 0;
  43.   int right = initialNum;
  44.   while (left <= right) {
  45.     int middle = (left + right) / 2;
  46.     vector<vector<int>> neighbours = SelectAppropriateGraph(neighboursWithWeight, middle, n);
  47.     if (NumOfComponents(neighbours, n) == initialNum) { // то есть можно попробовать больше снести
  48.       left = middle;
  49.     } else { // то есть надо меньше снести
  50.       right = middle;
  51.     }
  52.   }
  53. }
  54.  
  55. int BinarySearch(vector<bool>& a) {
  56.   int left = 0;
  57.   int right = a.size() - 1;
  58.   while (left <= right) {
  59.     int middle = (left + right) / 2;
  60.     if (a[middle]) {
  61.       left = middle + 1;
  62.     } else {
  63.       right = middle - 1;
  64.     }
  65.   }
  66.   if (a[left]) return left;
  67.   return left - 1;
  68. }
  69.  
  70. int main() {
  71. //  int n, m;
  72. //  std::cin >> n >> m;
  73. //  vector<vector<std::pair<int, int>>> neighboursWithWeight(n);
  74. //  vector<vector<int>> neighbours(n);
  75. //  int maxWeight = 0;
  76. //  for (int j = 0; j < m; ++j) {
  77. //    int l, r, w;
  78. //    std::cin >> l >> r >> w;
  79. //    maxWeight = std::max(maxWeight, w);
  80. //    if (l == r) continue; // ok?
  81. //    neighboursWithWeight[l - 1].emplace_back(r - 1, w);
  82. //    neighboursWithWeight[r - 1].emplace_back(l - 1, w);
  83. //    neighbours[l - 1].push_back(r - 1);
  84. //    neighbours[r - 1].push_back(l - 1);
  85. //  }
  86. //  int initialNumOfComponents = NumOfComponents(neighbours, n);
  87.   vector<bool> a = {1, 1, 0, 0};
  88.   std::cout << BinarySearch(a) + 1;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement