Submission #503713

#TimeUsernameProblemLanguageResultExecution timeMemory
503713StickfishPaths (RMI21_paths)C++17
36 / 100
1088 ms12144 KiB
#include <iostream> #include <vector> using namespace std; using ll = long long; const int MAXN = 100008; vector<pair<int, ll>> edg[MAXN]; vector<ll> prof[MAXN]; vector<ll> merge(vector<ll>& a, vector<ll>& b) { int n = a.size(); int m = b.size(); vector<ll> ans(n + m - 1); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { ans[i + j] = max(ans[i + j], a[i] + b[j]); } } return ans; } vector<ll> dfs(int v, int rt) { if (edg[v].empty() || edg[v].size() == 1 && edg[v][0].first == rt) { return {0, 0}; } vector<vector<ll>> prof_edg; for (auto [u, d] : edg[v]) { if (u == rt) continue; prof_edg.push_back(dfs(u, v)); for (auto& x : prof_edg.back()) { x += d; } prof_edg.back()[0] -= d; } while (prof_edg.size() > 1) { int n = prof_edg.size(); vector<vector<ll>> new_profedg; for (int i = 0; i + 1 < n; i += 2) { new_profedg.push_back(merge(prof_edg[i], prof_edg[i + 1])); } if (n % 2) { new_profedg.push_back(prof_edg.back()); } prof_edg = new_profedg; } return prof_edg.back(); } signed main() { int n, k; cin >> n >> k; for (int i = 0; i + 1 < n; ++i) { int u, v, c; cin >> u >> v >> c; --u, --v; edg[u].push_back({v, c}); edg[v].push_back({u, c}); } for (int i = 0; i < n; ++i) { vector<ll> ans = dfs(i, -1); if (k >= ans.size()) cout << ans.back() << '\n'; else cout << ans[k] << '\n'; } }

Compilation message (stderr)

Main.cpp: In function 'std::vector<long long int> dfs(int, int)':
Main.cpp:23:46: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   23 |     if (edg[v].empty() || edg[v].size() == 1 && edg[v][0].first == rt) {
      |                           ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:62:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |         if (k >= ans.size())
      |             ~~^~~~~~~~~~~~~
#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...