Advertisement
kdzhr

1018

Mar 19th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. // WA3 https://acm.timus.ru/problem.aspx?space=1&num=1018        ̶Н̶о̶р̶м̶а̶л̶ь̶н̶о̶ ̶д̶е̶л̶а̶й̶ ̶-̶ ̶н̶о̶р̶м̶а̶л̶ь̶н̶о̶ ̶б̶у̶д̶е̶т̶
  2.  
  3. # include <iostream>
  4. # include <vector>
  5.  
  6. struct branch {
  7.     size_t from;
  8.     size_t to;
  9.     size_t count;
  10.     bool online = true;
  11. };
  12.  
  13. int main() {
  14.     size_t n, q;
  15.     std::cin >> n >> q;
  16.     std::vector<branch> tree;
  17.     std::vector<size_t> count_of_children(n + 1, 0);
  18.     std::vector<bool> init_v(n, false);
  19.     std::vector<size_t> res;
  20.     size_t sum = 0;
  21.     res.push_back(0);
  22.     for (size_t i = 0; i < n - 1; i++) {
  23.         size_t a, b, c;
  24.         std::cin >> a >> b >> c;
  25.         sum += c;
  26.         if (init_v[b]) {
  27.             size_t cur;
  28.             cur = a;
  29.             a = b;
  30.             b = cur;
  31.         }
  32.         init_v[a] = true;
  33.         init_v[b] = true;
  34.         branch x;
  35.         x.from = a;
  36.         x.to = b;
  37.         x.count = c;
  38.         x.online = true;
  39.         count_of_children[a]++;
  40.         tree.push_back(x);
  41.     }
  42.     for (size_t i = 0; i < (n - 1 - q); i++) {
  43.         int32_t min_br_j = -1;
  44.         for (size_t j = 0; j < tree.size(); j++) {
  45.             if (!tree[j].online) continue;
  46.             if (count_of_children[tree[j].to] == 0 && (tree[j].count < tree[min_br_j].count || min_br_j == -1)) {
  47.                 min_br_j = j;
  48.             }
  49.         }
  50.         res.push_back(res.back() + tree[min_br_j].count);
  51.         tree[min_br_j].online = false;
  52.         count_of_children[tree[min_br_j].from]--;
  53.     }
  54.     std::cout << sum - res.back() << '\n';
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement