SansPapyrus683

Erroneous Solution to Oil Business

Feb 13th, 2022 (edited)
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using std::cout;
  6. using std::endl;
  7. using std::vector;
  8.  
  9. class DisjointSets {
  10.     private:
  11.         vector<int> parents;
  12.         vector<int> sizes;
  13.     public:
  14.         DisjointSets(int size) : parents(size), sizes(size, 1) {
  15.             for (int i = 0; i < size; i++) {
  16.                 parents[i] = i;
  17.             }
  18.         }
  19.  
  20.         int get_ultimate(int n) {
  21.             return parents[n] == n ? n : (parents[n] = get_ultimate(parents[n]));
  22.         }
  23.  
  24.         bool link(int n1, int n2) {
  25.             n1 = get_ultimate(n1);
  26.             n2 = get_ultimate(n2);
  27.             if (n1 == n2) {
  28.                 return false;
  29.             }
  30.             if (sizes[n1] < sizes[n2]) {
  31.                 std::swap(n1, n2);
  32.             }
  33.             sizes[n1] += sizes[n2];
  34.             parents[n2] = n1;
  35.             return true;
  36.         }
  37. };
  38.  
  39. struct Road {
  40.     int ind;
  41.     int a, b;
  42.     int cost;
  43. };
  44.  
  45. /**
  46.  * https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/problem/H
  47.  * (sample input omitted due to length)
  48.  */
  49. int main() {
  50.     int city_num;
  51.     int road_num;
  52.     long long total_cost;
  53.     std::cin >> city_num >> road_num >> total_cost;
  54.     vector<Road> roads(road_num);
  55.     for (int i = 0; i < road_num; i++) {
  56.         Road& r = roads[i];
  57.         r.ind = i + 1;  // stupid 1-indexing
  58.         std::cin >> r.a >> r.b >> r.cost;
  59.         r.a--;
  60.         r.b--;
  61.     }
  62.  
  63.     /*
  64.      * new problem:
  65.      * min # of edges so that the graph's connected and
  66.      * the edge cost >= total_cost
  67.      */
  68.     std::sort(
  69.         roads.begin(), roads.end(),
  70.         [&] (const Road& r1, const Road& r2) { return r1.cost < r2.cost; }
  71.     );
  72.     DisjointSets spanning_tree(city_num);
  73.     int added = 0;
  74.     vector<bool> added_roads(road_num);
  75.     long long cost = 0;
  76.     for (int i = road_num - 1; i >= 0; i--) {
  77.         const Road& r = roads[i];
  78.         bool res = spanning_tree.link(r.a, r.b);
  79.         added_roads[i] = res;
  80.         added += res;
  81.         cost += res * r.cost;
  82.         if (added == road_num - 1) {
  83.             break;
  84.         }
  85.     }
  86.  
  87.     for (int r = road_num - 1; r >= 0; r--) {
  88.         if (cost >= total_cost) {
  89.             break;
  90.         }
  91.         if (!added_roads[r]) {
  92.             added_roads[r] = true;
  93.             cost += roads[r].cost;
  94.             added++;
  95.         }
  96.     }
  97.  
  98.     cout << road_num - added << endl;
  99.     for (int r = 0; r < road_num; r++) {
  100.         if (!added_roads[r]) {
  101.             cout << roads[r].ind << ' ';
  102.         }
  103.     }
  104.     cout << endl;
  105. }
  106.  
Add Comment
Please, Sign In to add comment