Skip to content

Commit

Permalink
[update] V = E
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo314 committed Jun 29, 2024
1 parent 8057174 commit 609038c
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions cpp/rerooting-dp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
#include <algorithm>
#include <type_traits>

#include "tree.hpp"

/**
Expand All @@ -22,7 +21,7 @@
* @param addnode (E,int)->V
* @return std::vector<V>
*/
template <typename E, typename V, class Merge, class AddEdge, class AddNode, typename Cost>
template <typename E, typename V = E, class Merge, class AddEdge, class AddNode, typename Cost>
std::vector<V> rerooting_dp(const Tree<Cost>& tree, E e, Merge merge, AddEdge addedge, AddNode addnode) {
static_assert(std::is_invocable_r_v<E, Merge, E, E>);
static_assert(std::is_invocable_r_v<E, AddEdge, V, Cost, int>);
Expand Down Expand Up @@ -59,7 +58,7 @@ std::vector<V> rerooting_dp(const Tree<Cost>& tree, E e, Merge merge, AddEdge ad
*/
template <typename Cost>
inline std::vector<Cost> farthest_node_dist(const Tree<Cost>& tree) {
return rerooting_dp<Cost, Cost>(
return rerooting_dp(
tree,
Cost{},
[](Cost a, Cost b) { return std::max<Cost>(a, b); },
Expand All @@ -73,7 +72,7 @@ inline std::vector<Cost> farthest_node_dist(const Tree<Cost>& tree) {
template <typename Cost>
inline std::vector<Cost> distance_sums(const Tree<Cost>& tree) {
using P = typename std::pair<Cost, int>;
auto tmp = rerooting_dp<P, P>(
auto tmp = rerooting_dp(
tree,
P{{}, 0},
[](P a, P b) { return P{a.first + b.first, a.second + b.second}; },
Expand All @@ -90,7 +89,7 @@ inline std::vector<Cost> distance_sums(const Tree<Cost>& tree) {
*/
template <typename V, typename Cost>
inline std::vector<V> connected_subgraph_count(const Tree<Cost>& tree) {
return rerooting_dp<V, V>(
return rerooting_dp(
tree,
V(1),
[](V a, V b) { return a * b; },
Expand Down

0 comments on commit 609038c

Please sign in to comment.