This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ebi-fly13/Library
#define PROBLEM "https://judge.yosupo.jp/problem/tree_path_composite_sum" #include <iostream> #include <vector> #include "../../graph/base.hpp" #include "../../modint/modint.hpp" #include "../../tree/rerooting.hpp" using mint = ebi::modint998244353; std::vector<mint> a; struct S { mint sum; mint sz; }; S e() { return {0, 0}; } S merge(S lhs, S rhs) { return {lhs.sum + rhs.sum, lhs.sz + rhs.sz}; } S put_edge(std::pair<mint, mint> e, S x) { auto [b, c] = e; return {b * x.sum + c * x.sz, x.sz}; } S put_root(int idx, S x) { return {a[idx] + x.sum, x.sz + 1}; } int main() { int n; std::cin >> n; for (int i = 0; i < n; i++) { int val; std::cin >> val; a.emplace_back(val); } ebi::Graph<std::pair<mint, mint>> g(n); for (int i = 0; i < n - 1; i++) { int u, v, b, c; std::cin >> u >> v >> b >> c; g.add_edge(u, v, {b, c}); g.add_edge(v, u, {b, c}); } g.build(); ebi::rerooting<std::pair<mint, mint>, S, S, e, merge, put_edge, put_root> dp(n, g); for (int i = 0; i < n; i++) { std::cout << dp.get(i).sum.val() << " \n"[i == n - 1]; } }
#line 1 "test/tree/Tree_Path_Composite_Sum.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/tree_path_composite_sum" #include <iostream> #include <vector> #line 2 "graph/base.hpp" #include <cassert> #line 5 "graph/base.hpp" #include <ranges> #line 7 "graph/base.hpp" #line 2 "data_structure/simple_csr.hpp" #line 4 "data_structure/simple_csr.hpp" #include <utility> #line 6 "data_structure/simple_csr.hpp" namespace ebi { template <class E> struct simple_csr { simple_csr() = default; simple_csr(int n, const std::vector<std::pair<int, E>>& elements) : start(n + 1, 0), elist(elements.size()) { for (auto e : elements) { start[e.first + 1]++; } for (auto i : std::views::iota(0, n)) { start[i + 1] += start[i]; } auto counter = start; for (auto [i, e] : elements) { elist[counter[i]++] = e; } } simple_csr(const std::vector<std::vector<E>>& es) : start(es.size() + 1, 0) { int n = es.size(); for (auto i : std::views::iota(0, n)) { start[i + 1] = (int)es[i].size() + start[i]; } elist.resize(start.back()); for (auto i : std::views::iota(0, n)) { std::copy(es[i].begin(), es[i].end(), elist.begin() + start[i]); } } int size() const { return (int)start.size() - 1; } const auto operator[](int i) const { return std::ranges::subrange(elist.begin() + start[i], elist.begin() + start[i + 1]); } auto operator[](int i) { return std::ranges::subrange(elist.begin() + start[i], elist.begin() + start[i + 1]); } const auto operator()(int i, int l, int r) const { return std::ranges::subrange(elist.begin() + start[i] + l, elist.begin() + start[i + 1] + r); } auto operator()(int i, int l, int r) { return std::ranges::subrange(elist.begin() + start[i] + l, elist.begin() + start[i + 1] + r); } private: std::vector<int> start; std::vector<E> elist; }; } // namespace ebi #line 9 "graph/base.hpp" namespace ebi { template <class T> struct Edge { int from, to; T cost; int id; }; template <class E> struct Graph { using cost_type = E; using edge_type = Edge<cost_type>; Graph(int n_) : n(n_) {} Graph() = default; void add_edge(int u, int v, cost_type c) { buff.emplace_back(u, edge_type{u, v, c, m}); edges.emplace_back(edge_type{u, v, c, m++}); } void add_undirected_edge(int u, int v, cost_type c) { buff.emplace_back(u, edge_type{u, v, c, m}); buff.emplace_back(v, edge_type{v, u, c, m}); edges.emplace_back(edge_type{u, v, c, m}); m++; } void read_tree(int offset = 1, bool is_weighted = false) { read_graph(n - 1, offset, false, is_weighted); } void read_parents(int offset = 1) { for (auto i : std::views::iota(1, n)) { int p; std::cin >> p; p -= offset; add_undirected_edge(p, i, 1); } build(); } void read_graph(int e, int offset = 1, bool is_directed = false, bool is_weighted = false) { for (int i = 0; i < e; i++) { int u, v; std::cin >> u >> v; u -= offset; v -= offset; if (is_weighted) { cost_type c; std::cin >> c; if (is_directed) { add_edge(u, v, c); } else { add_undirected_edge(u, v, c); } } else { if (is_directed) { add_edge(u, v, 1); } else { add_undirected_edge(u, v, 1); } } } build(); } void build() { assert(!prepared); csr = simple_csr<edge_type>(n, buff); buff.clear(); prepared = true; } int size() const { return n; } int node_number() const { return n; } int edge_number() const { return m; } edge_type get_edge(int i) const { return edges[i]; } std::vector<edge_type> get_edges() const { return edges; } const auto operator[](int i) const { return csr[i]; } auto operator[](int i) { return csr[i]; } private: int n, m = 0; std::vector<std::pair<int,edge_type>> buff; std::vector<edge_type> edges; simple_csr<edge_type> csr; bool prepared = false; }; } // namespace ebi #line 2 "modint/modint.hpp" #line 5 "modint/modint.hpp" #line 2 "modint/base.hpp" #include <concepts> #line 6 "modint/base.hpp" namespace ebi { template <class T> concept Modint = requires(T a, T b) { a + b; a - b; a * b; a / b; a.inv(); a.val(); a.pow(std::declval<long long>()); T::mod(); }; template <Modint mint> std::istream &operator>>(std::istream &os, mint &a) { long long x; os >> x; a = x; return os; } template <Modint mint> std::ostream &operator<<(std::ostream &os, const mint &a) { return os << a.val(); } } // namespace ebi #line 7 "modint/modint.hpp" namespace ebi { template <int m> struct static_modint { private: using modint = static_modint; public: static constexpr int mod() { return m; } static constexpr modint raw(int v) { modint x; x._v = v; return x; } constexpr static_modint() : _v(0) {} constexpr static_modint(long long v) { v %= (long long)umod(); if (v < 0) v += (long long)umod(); _v = (unsigned int)v; } constexpr unsigned int val() const { return _v; } constexpr unsigned int value() const { return val(); } constexpr modint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } constexpr modint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } constexpr modint operator++(int) { modint res = *this; ++*this; return res; } constexpr modint operator--(int) { modint res = *this; --*this; return res; } constexpr modint &operator+=(const modint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } constexpr modint &operator-=(const modint &rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } constexpr modint &operator*=(const modint &rhs) { unsigned long long x = _v; x *= rhs._v; _v = (unsigned int)(x % (unsigned long long)umod()); return *this; } constexpr modint &operator/=(const modint &rhs) { return *this = *this * rhs.inv(); } constexpr modint operator+() const { return *this; } constexpr modint operator-() const { return modint() - *this; } constexpr modint pow(long long n) const { assert(0 <= n); modint x = *this, res = 1; while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } constexpr modint inv() const { assert(_v); return pow(umod() - 2); } friend modint operator+(const modint &lhs, const modint &rhs) { return modint(lhs) += rhs; } friend modint operator-(const modint &lhs, const modint &rhs) { return modint(lhs) -= rhs; } friend modint operator*(const modint &lhs, const modint &rhs) { return modint(lhs) *= rhs; } friend modint operator/(const modint &lhs, const modint &rhs) { return modint(lhs) /= rhs; } friend bool operator==(const modint &lhs, const modint &rhs) { return lhs.val() == rhs.val(); } friend bool operator!=(const modint &lhs, const modint &rhs) { return !(lhs == rhs); } private: unsigned int _v = 0; static constexpr unsigned int umod() { return m; } }; using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; } // namespace ebi #line 2 "tree/rerooting.hpp" #line 6 "tree/rerooting.hpp" #line 8 "tree/rerooting.hpp" namespace ebi { template <class T, class V, class E, E (*e)(), E (*merge)(E, E), E (*put_edge)(T, V), V (*put_root)(int, E)> struct rerooting { private: V dfs_sub(int v, int par = -1) { E ret = e(); for (auto &edge : g[v]) { if (edge.to == par && g[v].back().to != par) std::swap(g[v].back(), edge); if (edge.to == par) continue; E val = put_edge(edge.cost, dfs_sub(edge.to, v)); outs[v].emplace_back(val); ret = merge(ret, val); } sub[v] = put_root(v, ret); return sub[v]; } void dfs_all(int v, int par = -1, E rev = e()) { int sz = outs[v].size(); std::vector<E> lcum(sz + 1, e()), rcum(sz + 1, e()); for (int i = 0; i < sz; i++) { lcum[i + 1] = merge(lcum[i], outs[v][i]); rcum[sz - i - 1] = merge(rcum[sz - i], outs[v][sz - i - 1]); } for (int i = 0; i < sz; i++) { auto edge = g[v][i]; E ret = put_edge(edge.cost, put_root(v, merge(merge(lcum[i], rcum[i + 1]), rev))); dfs_all(edge.to, v, ret); } dp[v] = put_root(v, merge(lcum[sz], rev)); } public: rerooting(int n, const Graph<T> &g_) : n(n), g(g_), sub(n), dp(n), outs(n) { dfs_sub(0); dfs_all(0); } V get(int v) const { return dp[v]; } private: int n; Graph<T> g; std::vector<V> sub; std::vector<V> dp; std::vector<std::vector<E>> outs; }; } // namespace ebi #line 9 "test/tree/Tree_Path_Composite_Sum.test.cpp" using mint = ebi::modint998244353; std::vector<mint> a; struct S { mint sum; mint sz; }; S e() { return {0, 0}; } S merge(S lhs, S rhs) { return {lhs.sum + rhs.sum, lhs.sz + rhs.sz}; } S put_edge(std::pair<mint, mint> e, S x) { auto [b, c] = e; return {b * x.sum + c * x.sz, x.sz}; } S put_root(int idx, S x) { return {a[idx] + x.sum, x.sz + 1}; } int main() { int n; std::cin >> n; for (int i = 0; i < n; i++) { int val; std::cin >> val; a.emplace_back(val); } ebi::Graph<std::pair<mint, mint>> g(n); for (int i = 0; i < n - 1; i++) { int u, v, b, c; std::cin >> u >> v >> b >> c; g.add_edge(u, v, {b, c}); g.add_edge(v, u, {b, c}); } g.build(); ebi::rerooting<std::pair<mint, mint>, S, S, e, merge, put_edge, put_root> dp(n, g); for (int i = 0; i < n; i++) { std::cout << dp.get(i).sum.val() << " \n"[i == n - 1]; } }