Advertisement
den4ik2003

Untitled

Oct 29th, 2023
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 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.       DFS(i, neighbours, used, num_of_component++);
  23.     }
  24.   }
  25.   return num_of_component - 1;
  26. }
  27.  
  28. vector<vector<int>> SelectAppropriateGraph(vector<vector<std::pair<int, int>>>& neighboursWithWeight, int x, int n) {
  29.   vector<vector<int>> appropriateGraph(n);
  30.   for (int i = 0; i < neighboursWithWeight.size(); ++i) {
  31.     for (int j = 0; j < neighboursWithWeight[i].size(); ++j) {
  32.       if (neighboursWithWeight[i][j].second > x) {
  33.         appropriateGraph[i].push_back(neighboursWithWeight[i][j].first);
  34.       }
  35.     }
  36.   }
  37.   return std::move(appropriateGraph);
  38. }
  39.  
  40. int BinaryFindAnswer(vector<vector<std::pair<int, int>>>& neighboursWithWeight, int max, int initialNum, int n) {
  41.   int left = 0;
  42.   int right = max;
  43.   while (left <= right) {
  44.     int middle = (left + right) / 2;
  45.     vector<vector<int>> neighbours = SelectAppropriateGraph(neighboursWithWeight, middle, n);
  46.     if (NumOfComponents(neighbours, n) == initialNum) { // то есть можно попробовать больше снести
  47.       left = middle + 1;
  48.     } else { // то есть надо меньше снести
  49.       right = middle - 1;
  50.     }
  51.   }
  52.   vector<vector<int>> neighbours = SelectAppropriateGraph(neighboursWithWeight, left, n);
  53.   if (NumOfComponents(neighbours, n) == initialNum) return left;
  54.   return left - 1;
  55. }
  56.  
  57. int main() {
  58.   int n, m;
  59.   std::cin >> n >> m;
  60.   vector<vector<std::pair<int, int>>> neighboursWithWeight(n);
  61.   vector<vector<int>> neighbours(n);
  62.   int maxWeight = 0;
  63.   for (int j = 0; j < m; ++j) {
  64.     int l, r, w;
  65.     std::cin >> l >> r >> w;
  66.     maxWeight = std::max(maxWeight, w);
  67.     if (l == r) continue; // ok?
  68.     neighboursWithWeight[l - 1].emplace_back(r - 1, w);
  69.     neighboursWithWeight[r - 1].emplace_back(l - 1, w);
  70.     neighbours[l - 1].push_back(r - 1);
  71.     neighbours[r - 1].push_back(l - 1);
  72.   }
  73.   int initialNumOfComponents = NumOfComponents(neighbours, n);
  74.   std::cout << BinaryFindAnswer(neighboursWithWeight, maxWeight, initialNumOfComponents, n);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement