kyopro_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub dyktr06/kyopro_library

:heavy_check_mark: test/library_checker/tree/tree_path_composite_sum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/tree_path_composite_sum"
#include <bits/stdc++.h>
using namespace std;

#include "../../../lib/graph/rerooting.hpp"
#include "../../../lib/math/modint.hpp"

using mint = ModInt<998244353>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n; cin >> n;
    vector<mint> a(n), b(n - 1), c(n - 1);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    using T = pair<mint, mint>;
    auto merge = [&](T x, T y){
        T res = {x.first + y.first, x.second + y.second};
        return res;
    };
    auto put_edge = [&](T x, int i){
        T res = {x.first * b[i] + x.second * c[i], x.second};
        return res;
    };
    auto put_vertex = [&](T e, int v){
        T res = {e.first + a[v], e.second + 1};
        return res;
    };
    T e = {0, 0};
    Rerooting<T, T> G(n, merge, e, put_edge, put_vertex);
    for(int i = 0; i < n - 1; i++){
        int u, v; cin >> u >> v >> b[i] >> c[i];
        G.add_edge(u, v, i, i);
    }
    G.build();
    for(auto &ans : G.reroot()){
        cout << ans.first << " ";
    }
    cout << "\n";
}
#line 1 "test/library_checker/tree/tree_path_composite_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/tree_path_composite_sum"
#include <bits/stdc++.h>
using namespace std;

#line 2 "lib/graph/rerooting.hpp"

/**
 * @brief Rerooting (全方位木DP)
 * @see https://trap.jp/post/1702/
 */

template<typename E, typename V>
struct Rerooting{
    struct edge{
        int from, to, idx, rev_idx;
    };
    int n, root;
    vector<vector<edge>> edges;
    vector<int> visited;
    vector<vector<E>> out;
    vector<E> reverse_edge;
    vector<V> ans;
    function<E(E, E)> merge;
    E e;
    function<E(V, int)> put_edge;
    function<V(E, int)> put_vertex;
    Rerooting(int _n, const function<E(E, E)> &_merge, const E &_e, const function<E(V, int)> &_put_edge, const function<V(E, int)> &_put_vertex) : n(_n), merge(_merge), e(_e), put_edge(_put_edge), put_vertex(_put_vertex){
        edges.resize(n);
    }

private:
    V dfs(int v){
        visited[v]++;
        E val = e;
        for(auto &p : edges[v]){
            if(visited[p.to] > 0 && p.to != edges[v].back().to){
                swap(p, edges[v].back());
            }
            if(visited[p.to] > 0) continue;
            E nval = put_edge(dfs(p.to), p.idx);
            out[v].emplace_back(nval);
            val = merge(val, nval);
        }
        return put_vertex(val, v);
    }

    void bfs(int v){
        int siz = out[v].size();
        vector<E> left(siz + 1), right(siz + 1);
        left[0] = e, right[siz] = e;
        for(int i = 0; i < siz; i++){
            left[i + 1] = merge(left[i], out[v][i]);
        }
        for(int i = siz - 1; i >= 0; i--){
            right[i] = merge(out[v][i], right[i + 1]);
        }
        for(int i = 0; i < siz; i++){
            reverse_edge[edges[v][i].to] = put_edge(put_vertex(merge(merge(left[i], right[i + 1]), reverse_edge[v]), v), edges[v][i].rev_idx);
            bfs(edges[v][i].to);
        }
        ans[v] = put_vertex(merge(left[siz], reverse_edge[v]), v);
    }

public:
    void add_edge(int u, int v, int idx1, int idx2){
        edges[u].push_back({u, v, idx1, idx2});
        edges[v].push_back({v, u, idx2, idx1});
    }

    V build(int v = 0){
        root = v;
        out.resize(n);
        visited.assign(n, 0);
        return dfs(root);
    }

    vector<V> reroot(){
        reverse_edge.resize(n);
        reverse_edge[root] = e;
        ans.resize(n);
        bfs(root);
        return ans;
    }
};
#line 2 "lib/math/modint.hpp"

#line 5 "lib/math/modint.hpp"

/**
 * @brief ModInt
 * @docs docs/math/modint.md
 */

template <long long Modulus>
struct ModInt{
    long long val;
    static constexpr int mod() { return Modulus; }
    constexpr ModInt(const long long _val = 0) noexcept : val(_val) {
        normalize();
    }
    void normalize(){
        val = (val % Modulus + Modulus) % Modulus;
    }
    inline ModInt &operator+=(const ModInt &rhs) noexcept {
        if(val += rhs.val, val >= Modulus) val -= Modulus;
        return *this;
    }
    inline ModInt &operator-=(const ModInt &rhs) noexcept {
        if(val -= rhs.val, val < 0) val += Modulus;
        return *this;
    }
    inline ModInt &operator*=(const ModInt &rhs) noexcept {
        val = val * rhs.val % Modulus;
        return *this;
    }
    inline ModInt &operator/=(const ModInt &rhs) noexcept {
        val = val * inv(rhs.val).val % Modulus;
        return *this;
    }
    inline ModInt &operator++() noexcept {
        if(++val >= Modulus) val -= Modulus;
        return *this;
    }
    inline ModInt operator++(int) noexcept {
        ModInt t = val;
        if(++val >= Modulus) val -= Modulus;
        return t;
    }
    inline ModInt &operator--() noexcept {
        if(--val < 0) val += Modulus;
        return *this;
    }
    inline ModInt operator--(int) noexcept {
        ModInt t = val;
        if(--val < 0) val += Modulus;
        return t;
    }
    inline ModInt operator-() const noexcept { return (Modulus - val) % Modulus; }
    inline ModInt inv(void) const { return inv(val); }
    ModInt pow(long long n) const {
        assert(0 <= n);
        ModInt x = *this, r = 1;
        while(n){
            if(n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    ModInt inv(const long long n) const {
        long long a = n, b = Modulus, u = 1, v = 0;
        while(b){
            long long t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        u %= Modulus;
        if(u < 0) u += Modulus;
        return u;
    }
    friend inline ModInt operator+(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) += rhs; }
    friend inline ModInt operator-(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) -= rhs; }
    friend inline ModInt operator*(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) *= rhs; }
    friend inline ModInt operator/(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) /= rhs; }
    friend inline bool operator==(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val == rhs.val; }
    friend inline bool operator!=(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val != rhs.val; }
    friend inline std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
        is >> x.val;
        x.normalize();
        return is;
    }
    friend inline std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept { return os << x.val; }
};
#line 7 "test/library_checker/tree/tree_path_composite_sum.test.cpp"

using mint = ModInt<998244353>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n; cin >> n;
    vector<mint> a(n), b(n - 1), c(n - 1);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    using T = pair<mint, mint>;
    auto merge = [&](T x, T y){
        T res = {x.first + y.first, x.second + y.second};
        return res;
    };
    auto put_edge = [&](T x, int i){
        T res = {x.first * b[i] + x.second * c[i], x.second};
        return res;
    };
    auto put_vertex = [&](T e, int v){
        T res = {e.first + a[v], e.second + 1};
        return res;
    };
    T e = {0, 0};
    Rerooting<T, T> G(n, merge, e, put_edge, put_vertex);
    for(int i = 0; i < n - 1; i++){
        int u, v; cin >> u >> v >> b[i] >> c[i];
        G.add_edge(u, v, i, i);
    }
    G.build();
    for(auto &ans : G.reroot()){
        cout << ans.first << " ";
    }
    cout << "\n";
}
Back to top page