제출 #578557

#제출 시각아이디문제언어결과실행 시간메모리
578557maomao90Paths (RMI21_paths)C++17
56 / 100
1094 ms67300 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n, k; vector<pair<int, int>> adj[MAXN]; priority_queue<long long> dp[MAXN]; void dfs(int u, int p) { while (!dp[u].empty()) { dp[u].pop(); } for (auto [v, w] : adj[u]) { if (v == p) continue; dfs(v, u); long long x = dp[v].top(); dp[v].pop(); dp[v].push(x + w); if (dp[u].size() < dp[v].size()) { swap(dp[u], dp[v]); } while (!dp[v].empty()) { long long x = dp[v].top(); dp[v].pop(); dp[u].push(x); } } if (dp[u].empty()) { dp[u].push(0); } } int main() { #ifndef DEBUG ios::sync_with_stdio(0), cin.tie(0); #endif cin >> n >> k; for (int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; adj[a].push_back({b, c}); adj[b].push_back({a, c}); } for (int i = 1; i <= n; i++) { dfs(i, -1); long long ans = 0; int tk = k; while (!dp[i].empty() && tk--) { long long x = dp[i].top(); dp[i].pop(); ans += x; } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...