Advertisement
den4ik2003

Untitled

Sep 5th, 2023
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 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 main() {
  56.   int n, m;
  57.   std::cin >> n >> m;
  58.   vector<vector<std::pair<int, int>>> neighboursWithWeight(n);
  59.   vector<vector<int>> neighbours(n);
  60.   int maxWeight = 0;
  61.   for (int j = 0; j < m; ++j) {
  62.     int l, r, w;
  63.     std::cin >> l >> r >> w;
  64.     maxWeight = std::max(maxWeight, w);
  65.     if (l == r) continue; // ok?
  66.     neighboursWithWeight[l - 1].emplace_back(r - 1, w);
  67.     neighboursWithWeight[r - 1].emplace_back(l - 1, w);
  68.     neighbours[l - 1].push_back(r - 1);
  69.     neighbours[r - 1].push_back(l - 1);
  70.   }
  71.   int initialNumOfComponents = NumOfComponents(neighbours, n);
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement