Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- using namespace std;
- struct IdxCost{
- int idx;
- int cost;
- }
- int main() {
- int towns_count, cables_count;
- cin >> towns_count >> cables_count;
- vector <vector<IdxCost>> cables(1 + towns_count);
- for (int i = 0; i < cables_count; ++i) {
- int u, v, cost;
- cin >> u >> v >> cost;
- cables[u].emplace_back(v, cost);
- }
- Dsu dsu(1+ towns_count);
- vector<int> required_cables_idxs;
- int total_cost = 0;
- int required_cabels_count = 0;
- for (const auto[u, v, cost, idx] : cables) {
- if (dsu.GetParent(u) != dsu.GetParent(v)) {
- dsu.Union(u, v);
- total_cost += cost;
- ++required_cabels_count;
- required_cables_idxs.push_back(idx);
- }
- }
- sort(begin(required_cables_idxs), end(required_cables_idxs));
- cout << total_cost << ' ' << required_cabels_count << '\n';
- for (int idx : required_cables_idxs) cout << idx << ' ';
- return 0;
- }
- /*
- test1
- 2 2
- 1 2 3
- 1 2 4
- 3 1
- 1
- test2
- 3 3
- 1 2 5
- 1 3 10
- 3 2 4
- 14 2
- 2 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement