Submission #525945

# Submission time Handle Problem Language Result Execution time Memory
525945 2022-02-13T09:31:40 Z Josia Paths (RMI21_paths) C++14
0 / 100
600 ms 8592 KB
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;
#define int int64_t



vector<int> parents;
vector<vector<pair<int, int>>> graph;
int n, k;



void merge(priority_queue<int> &A, priority_queue<int> &B) {
    if (A.size() > B.size()) A.swap(B);
    int sizea = A.size();
    for (int i = 0; i<min((int)A.size(), k); i++) {B.push(A.top()); A.pop();}
}


priority_queue<int> dfs(int v, int p) {
    priority_queue<int> res;

    if (graph[v].size() == 1) res.push(0);

    if (parents[v] != -1) return res;

    parents[v] = p;

    for (auto u: graph[v]) {
        if (parents[u.first] != -1) continue;
        priority_queue<int> tmp = dfs(u.first, v);

        int newElem = tmp.top() + u.second;
        tmp.pop();
        tmp.push(newElem);

        merge(tmp, res);
    }

    return res;
}

void solve() {
    
    cin >> n >> k;
    graph.resize(n);
    parents.resize(n);

    for (int i = 0; i<n-1; i++) {
        int a, b, c; cin >> a >> b >> c;
        a--; b--;
        graph[a].push_back({b, c});
        graph[b].push_back({a, c});
    }

    for (int i = 0; i<n; i++) {
        fill(parents.begin(), parents.end(), -1);

        priority_queue<int> pq = dfs(i, -2);

        int res = 0;

        for (int i = 0; i<k; i++) {
            if (pq.empty()) break;
            res += pq.top();
            pq.pop();
        }

        cout << res << "\n";
    }
}

signed main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    solve();

    return 0;
}

Compilation message

Main.cpp: In function 'void merge(std::priority_queue<long int>&, std::priority_queue<long int>&)':
Main.cpp:18:9: warning: unused variable 'sizea' [-Wunused-variable]
   18 |     int sizea = A.size();
      |         ^~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1077 ms 8592 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -