11#pragma once
2- #include < vector>
2+ #include < vector>
33
44/* *
55 * @brief 全方位木DP
66 */
77
8- template <typename T,typename F,typename Fix>
9- struct reroot {
10- std::vector<std::vector<long long >>g;
11- std::vector<int >p_list;
12- std::vector<T>p_table;
13- std::vector<bool >p_checked;
14- std::vector<map<int ,T>>table;
15- std::vector<T>ans;
8+ template <typename T, typename F, typename Fix> struct reroot {
9+ std::vector<std::vector<long long >> g;
10+ std::vector<int > p_list;
11+ std::vector<T> p_table;
12+ std::vector<bool > p_checked;
13+ std::vector<std::map<int , T>> table;
14+ std::vector<T> ans;
1615 T e;
1716 F f;
1817 Fix fix;
19- reroot (const std::vector<std::vector<long long >>& g,T e,F f=F(),Fix fix=Fix()):g(g),e(e),f(f),fix(fix){
20- int n=g.size ();
21- p_list.resize (n,-1 );
22- p_checked.resize (n,0 );
18+ reroot (const std::vector<std::vector<long long >>& g,
19+ T e,
20+ F f = F(),
21+ Fix fix = Fix())
22+ : g(g), e(e), f(f), fix(fix) {
23+ int n = g.size ();
24+ p_list.resize (n, -1 );
25+ p_checked.resize (n, 0 );
2326 table.resize (n);
24- p_table.resize (n,e);
25- ans.resize (n,e);
26- dfs1 (0 ,-1 );
27- for (int i= 0 ;i<n; ++i)ans[i]= dfs2 (i,-1 );
27+ p_table.resize (n, e);
28+ ans.resize (n, e);
29+ dfs1 (0 , -1 );
30+ for (int i = 0 ; i < n; ++i) ans[i] = dfs2 (i, -1 );
2831 }
29- T dfs1 (int n,int p){
30- p_list[n]= p;
31- T tmp1=e, tmp2= e;
32- std::vector<T>tmp (g[n].size ());
33- rep (i, g[n].size ()) {
34- int t= g[n][i];
35- if (t==p) continue ;
36- table[n][t]= tmp1;
37- tmp1= f (tmp1,tmp[i]= dfs1 (t,n));
32+ T dfs1 (int n, int p) {
33+ p_list[n] = p;
34+ T tmp1 = e, tmp2 = e;
35+ std::vector<T> tmp (g[n].size ());
36+ for ( int i = 0 ; i < g[n].size (); ++i) {
37+ int t = g[n][i];
38+ if (t == p) continue ;
39+ table[n][t] = tmp1;
40+ tmp1 = f (tmp1, tmp[i] = dfs1 (t, n));
3841 }
39- for (int i= g[n].size ()- 1 ;i>= 0 ; --i){
40- int t= g[n][i];
41- if (t==p) continue ;
42- table[n][t]= f (table[n][t],tmp2);
43- tmp2= f (tmp[i],tmp2);
42+ for (int i = g[n].size () - 1 ; i >= 0 ; --i) {
43+ int t = g[n][i];
44+ if (t == p) continue ;
45+ table[n][t] = f (table[n][t], tmp2);
46+ tmp2 = f (tmp[i], tmp2);
4447 }
45- return fix (table[n][p]= tmp1,n, p);
48+ return fix (table[n][p] = tmp1, n, p);
4649 }
47- T dfs2 (int n,int p){
48- if (n== -1 ){
50+ T dfs2 (int n, int p) {
51+ if (n == -1 ) {
4952 return e;
5053 }
51- if (!p_checked[n]){
52- p_checked[n]= 1 ;
53- p_table[n]= dfs2 (p_list[n],n);
54+ if (!p_checked[n]) {
55+ p_checked[n] = 1 ;
56+ p_table[n] = dfs2 (p_list[n], n);
5457 }
55- if (p==-1 ){
56- return f (table[n][p_list[n]],p_table[n]);
57- }else {
58- if (p_list[n]==-1 )return fix (table[n][p],n,p);
59- else return fix (f (table[n][p],p_table[n]),n,p);
58+ if (p == -1 ) {
59+ return f (table[n][p_list[n]], p_table[n]);
60+ } else {
61+ if (p_list[n] == -1 )
62+ return fix (table[n][p], n, p);
63+ else
64+ return fix (f (table[n][p], p_table[n]), n, p);
6065 }
6166 }
62- vector<T>query (){
63- return ans;
64- }
67+ std::vector<T> query () { return ans; }
6568};
0 commit comments