Fu_L's Library

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

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/aizu_online_judge/grl/height_of_a_tree.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_5_B"
#include "../../../src/template/template.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/tree/rerooting.hpp"
int main(void) {
    int n;
    cin >> n;
    Graph<int> g(n);
    map<pair<int, int>, int> mp;
    rep(i, 0, n - 1) {
        int a, b, c;
        cin >> a >> b >> c;
        g.add_edge(a, b, c);
        mp[{a, b}] = c;
        mp[{b, a}] = c;
    }
    using DP = int;
    auto f1 = [&](DP x, DP y) -> DP {
        return max(x, y);
    };
    auto f2 = [&](DP x, int child, int parent) -> DP {
        return x + mp[{child, parent}];
    };
    DP id = 0;
    vector<DP> dp = rerooting(g, f1, f2, id);
    rep(i, 0, n) {
        cout << dp[i] << '\n';
    }
}
#line 1 "verify/aizu_online_judge/grl/height_of_a_tree.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_5_B"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/tree/rerooting.hpp"
template <typename DP, typename T, typename F1, typename F2>
vector<DP> rerooting(const Graph<T>& g, const F1& f1, const F2& f2, const DP& id) {
    const int n = g.size();
    vector<DP> memo(n, id), dp(n, id);
    auto dfs = [&](const auto& dfs, const int cur, const int par) -> void {
        for(const Edge<T>& e : g[cur]) {
            if(e.to == par) continue;
            dfs(dfs, e.to, cur);
            memo[cur] = f1(memo[cur], f2(memo[e.to], e.to, cur));
        }
    };
    auto efs = [&](const auto& efs, const int cur, const int par, const DP& pval) -> void {
        vector<DP> buf;
        for(const Edge<T>& e : g[cur]) {
            if(e.to == par) continue;
            buf.emplace_back(f2(memo[e.to], e.to, cur));
        }
        vector<DP> head(buf.size() + 1), tail(buf.size() + 1);
        head[0] = tail[buf.size()] = id;
        for(int i = 0; i < (int)buf.size(); ++i) head[i + 1] = f1(head[i], buf[i]);
        for(int i = (int)buf.size() - 1; i >= 0; --i) {
            tail[i] = f1(tail[i + 1], buf[i]);
        }
        dp[cur] = par == -1 ? head.back() : f1(pval, head.back());
        int idx = 0;
        for(const Edge<T>& e : g[cur]) {
            if(e.to == par) continue;
            efs(efs, e.to, cur, f2(f1(pval, f1(head[idx], tail[idx + 1])), cur, e));
            ++idx;
        }
    };
    dfs(dfs, 0, -1);
    efs(efs, 0, -1, id);
    return dp;
}
#line 5 "verify/aizu_online_judge/grl/height_of_a_tree.test.cpp"
int main(void) {
    int n;
    cin >> n;
    Graph<int> g(n);
    map<pair<int, int>, int> mp;
    rep(i, 0, n - 1) {
        int a, b, c;
        cin >> a >> b >> c;
        g.add_edge(a, b, c);
        mp[{a, b}] = c;
        mp[{b, a}] = c;
    }
    using DP = int;
    auto f1 = [&](DP x, DP y) -> DP {
        return max(x, y);
    };
    auto f2 = [&](DP x, int child, int parent) -> DP {
        return x + mp[{child, parent}];
    };
    DP id = 0;
    vector<DP> dp = rerooting(g, f1, f2, id);
    rep(i, 0, n) {
        cout << dp[i] << '\n';
    }
}
Back to top page