Submission #858380

#TimeUsernameProblemLanguageResultExecution timeMemory
858380thangdz2k7Paths (RMI21_paths)C++17
56 / 100
1048 ms10064 KiB
#include <bits/stdc++.h> #define ii pair <int, int> #define F first #define S second using namespace std; const int mod = 1e9 + 7; const int N = 1e5 + 5; int n, k, dem; vector <ii> c[N]; long long ans[N]; long long dp[N]; void dfs(int u, int pa){ dp[u] = 0; for (ii v : c[u]){ if (v.F == pa) continue; dfs(v.F, u); dp[u] = max(dp[u], dp[v.F] + v.S); } bool used = false; for (ii v : c[u]){ if (v.F == pa) continue; if (used or dp[v.F] + v.S < dp[u]){ ++ dem; ans[dem] = dp[v.F] + v.S; } else used = true; } } long long solve_single_root(int r){ dem = 0; dfs(r, 0); ++ dem; ans[dem] = dp[r]; sort(ans + 1, ans + dem + 1); long long res = 0; for (int i = dem; i >= max(1, dem - k + 1); -- i) { res += ans[i]; //if (r == 1) cout << ans[i] << ' '; } return res; } void solve(){ cin >> n >> k; for (int i = 1; i <= n - 1; ++ i){ int u, v, w; cin >> u >> v >> w; c[u].push_back(ii(v, w)); c[v].push_back(ii(u, w)); } for (int i = 1; i <= n; ++ i) cout << solve_single_root(i) << '\n'; } int main(){ //freopen("code.inp", "r", stdin); //freopen("code.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t --) solve(); 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...