Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // WA3 https://acm.timus.ru/problem.aspx?space=1&num=1018 ̶Н̶о̶р̶м̶а̶л̶ь̶н̶о̶ ̶д̶е̶л̶а̶й̶ ̶-̶ ̶н̶о̶р̶м̶а̶л̶ь̶н̶о̶ ̶б̶у̶д̶е̶т̶
- # include <iostream>
- # include <vector>
- struct branch {
- size_t from;
- size_t to;
- size_t count;
- bool online = true;
- };
- int main() {
- size_t n, q;
- std::cin >> n >> q;
- std::vector<branch> tree;
- std::vector<size_t> count_of_children(n + 1, 0);
- std::vector<bool> init_v(n, false);
- std::vector<size_t> res;
- size_t sum = 0;
- res.push_back(0);
- for (size_t i = 0; i < n - 1; i++) {
- size_t a, b, c;
- std::cin >> a >> b >> c;
- sum += c;
- if (init_v[b]) {
- size_t cur;
- cur = a;
- a = b;
- b = cur;
- }
- init_v[a] = true;
- init_v[b] = true;
- branch x;
- x.from = a;
- x.to = b;
- x.count = c;
- x.online = true;
- count_of_children[a]++;
- tree.push_back(x);
- }
- for (size_t i = 0; i < (n - 1 - q); i++) {
- int32_t min_br_j = -1;
- for (size_t j = 0; j < tree.size(); j++) {
- if (!tree[j].online) continue;
- if (count_of_children[tree[j].to] == 0 && (tree[j].count < tree[min_br_j].count || min_br_j == -1)) {
- min_br_j = j;
- }
- }
- res.push_back(res.back() + tree[min_br_j].count);
- tree[min_br_j].online = false;
- count_of_children[tree[min_br_j].from]--;
- }
- std::cout << sum - res.back() << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement