-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rerooting-dp.hpp #120
Open
shogo314
wants to merge
12
commits into
main
Choose a base branch
from
rerooting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
rerooting-dp.hpp #120
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
14ba54f
[Add] rerooting-dp.hpp
shogo314 f7e0164
[fix] test and assert
shogo314 56170e0
[auto-verifier] verify commit f7e01645e7d709e46584fc5cc9424b4162fa3650
web-flow 0e95013
[update] delete unused i
shogo314 8057174
[auto-verifier] verify commit 0e95013302b6f70c97fca2ac21fe47d62c4ecd27
web-flow 609038c
[update] V = E
shogo314 66ff501
[auto-verifier] verify commit 609038c523f8222b0960d9df965b87e3cc0df1f2
web-flow cf3ee7c
[update] 冗長なmergeの簡略化
shogo314 489765d
[auto-verifier] verify commit cf3ee7c2cc82cc9c310d2a7c87a5658a4d2275ea
web-flow a521b75
doxygenno
shogo314 d541c3c
size_t -> std::size_t
shogo314 d09e1fe
[auto-verifier] verify commit d541c3c2e303a4bc4bd7bab99b0d5a1f53660fe5
web-flow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#pragma once | ||
|
||
/** | ||
* @file rerooting-dp.hpp | ||
* @brief 全方位木DP | ||
*/ | ||
#include <algorithm> | ||
#include <type_traits> | ||
#include "tree.hpp" | ||
|
||
/** | ||
* @brief 全方位木DP | ||
* dp[u] = addnode(merge(addedge(dp[v1], g_uv1, uv1.id), merge(addedge(dp[v2], g_uv2, uv2.id), ...)), u) v_iはuの子 | ||
* | ||
* @tparam E 可換モノイド | ||
* @tparam V DPの型 | ||
* @param tree | ||
* @param e 可換モノイドの単位元 | ||
* @param merge (E,E)->E 可換 | ||
* @param addedge (V,Cost,int)->E | ||
* @param addnode (E,int)->V | ||
* @return std::vector<V> | ||
*/ | ||
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>); | ||
static_assert(std::is_invocable_r_v<V, AddNode, E, int>); | ||
RootedTree<Cost> rooted(tree, 0); | ||
std::vector<V> subdp(rooted.n); | ||
for (int u : rooted.postorder) { | ||
E tmp = e; | ||
for (auto edge : rooted.child[u]) { | ||
tmp = merge(tmp, addedge(subdp[edge.dst], edge.cost, edge.id)); | ||
} | ||
subdp[u] = addnode(tmp, u); | ||
} | ||
std::vector<V> dp(rooted.n); | ||
std::vector<E> pe(rooted.n, e); | ||
for (int u : rooted.preorder) { | ||
const auto& ch = rooted.child[u]; | ||
std::vector<E> ri(ch.size() + 1, e); | ||
for (size_t i = ch.size(); i > 0; i--) { | ||
ri[i - 1] = merge(ri[i], addedge(subdp[ch[i - 1].dst], ch[i - 1].cost, ch[i - 1].id)); | ||
} | ||
dp[u] = addnode(merge(pe[u], ri[0]), u); | ||
E le = pe[u]; | ||
for (size_t i = 0; i < ch.size(); i++) { | ||
pe[ch[i].dst] = addedge(addnode(merge(le, ri[i + 1]), u), ch[i].cost, ch[i].id); | ||
le = merge(le, addedge(subdp[ch[i].dst], ch[i].cost, ch[i].id)); | ||
} | ||
} | ||
return dp; | ||
} | ||
|
||
/** | ||
* @brief 最も遠い頂点までの距離 | ||
*/ | ||
template <typename Cost> | ||
inline std::vector<Cost> farthest_node_dist(const Tree<Cost>& tree) { | ||
return rerooting_dp( | ||
tree, | ||
Cost{}, | ||
[](Cost a, Cost b) { return std::max<Cost>(a, b); }, | ||
[](Cost a, Cost c, int) { return a + c; }, | ||
[](Cost a, int) { return a; }); | ||
} | ||
|
||
/** | ||
* @brief 他の全ての頂点との距離の合計 | ||
*/ | ||
template <typename Cost> | ||
inline std::vector<Cost> distance_sums(const Tree<Cost>& tree) { | ||
using P = typename std::pair<Cost, int>; | ||
auto tmp = rerooting_dp( | ||
tree, | ||
P{{}, 0}, | ||
[](P a, P b) { return P{a.first + b.first, a.second + b.second}; }, | ||
[](P a, Cost c, int) { return P{a.first + a.second * c, a.second}; }, | ||
[](P a, int) { return P{a.first, a.second + 1}; }); | ||
std::vector<Cost> res; | ||
res.reserve(tree.n); | ||
for (const P& s : tmp) res.push_back(s.first); | ||
return res; | ||
} | ||
|
||
/** | ||
* @brief その頂点を含む連結な部分グラフの個数 | ||
*/ | ||
template <typename V, typename Cost> | ||
inline std::vector<V> connected_subgraph_count(const Tree<Cost>& tree) { | ||
return rerooting_dp( | ||
tree, | ||
V(1), | ||
[](V a, V b) { return a * b; }, | ||
[](V a, Cost c, int) { return a + 1; }, | ||
[](V a, int) { return a; }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1595" | ||
|
||
#include <iostream> | ||
|
||
#include "../cpp/rerooting-dp.hpp" | ||
|
||
int main() { | ||
int N; | ||
std::cin >> N; | ||
Tree<long long> tree(N); | ||
tree.read(); | ||
auto ans = farthest_node_dist(tree); | ||
for (int i = 0; i < N; i++) { | ||
std::cout << 2 * N - 2 - ans[i] << std::endl; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
結構どうでもいいんですが、C言語のsize_tよりC++のstd::size_tを使いたい