Problem solution · C++

Tree Path Composite Sum

Tree Path Composite Sum: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to NyaanNyaan Competitive Programming Library.

Technique
Dynamic programming
Source
NyaanNyaan Competitive Programming Library
Length
58 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Tree Path Composite Sum, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 58 lines of C++ from the credited upstream file yosupo-tree-path-composite-sum.test.cpp.
  • The implementation visibly relies on sequence storage, ordered lookup, cached states.
  • 1 loop block detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from NyaanNyaan Competitive Programming Library by NyaanNyaan and is used under the CC0-1.0 licence.

Full codeTree Path Composite Sum · C++C++
Use this to learn the idea, then write your own version.
#define PROBLEM "https://judge.yosupo.jp/problem/tree_path_composite_sum"//#include "../../template/template.hpp"//#include "../../graph/graph-template.hpp"//#include "../../tree/rerooting.hpp"//#include "../../modint/montgomery-modint.hpp"#include "../../modulo/binomial.hpp"//using namespace Nyaan;using mint = LazyMontgomeryModInt<998244353>;// using mint = LazyMontgomeryModInt<1000000007>;using vm = vector<mint>;using vvm = vector<vm>;Binomial<mint> C; using namespace Nyaan; void q() {  inl(N);  vm A(N);  in(A);   using pm = pair<mint, mint>;  vvi g(N);  map<pi, pm> mp;  rep(_, N - 1) {    ini(u, v, b, c);    g[u].push_back(v);    g[v].push_back(u);    mp[minmax(u, v)] = {b, c};  }   // 「T : 根が virtual である根付き木」に対応する情報を管理する  using T = pair<mint, int>;  // 空の状態に対応する情報  T leaf = {0, 0};  // T 同士をマージ  auto f1 = [&](T x, T y) -> T { return {x.fi + y.fi, x.se + y.se}; };  // T の根に頂点 c および辺 c-p を追加する (p は virtual)  auto f2 = [&](T xn, int c, int p) -> T {    auto [x, n] = xn;    auto [a, b] = mp[minmax(c, p)];    return make_pair((x + A[c]) * a + b * (n + 1), n + 1);  };  Rerooting<T, decltype(g), decltype(f1), decltype(f2)> dp(g, f1, f2, leaf);  auto ans = dp.dp;  rep(i, N) cout << (ans[i].fi + A[i]) << " \n"[i + 1 == N];} void Nyaan::solve() {  int t = 1;  // in(t);  while (t--) q();} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗