-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrerooting_with_edge.cpp
More file actions
354 lines (324 loc) · 10.8 KB
/
Copy pathrerooting_with_edge.cpp
File metadata and controls
354 lines (324 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// 抽象化した全方位木 DP (木 DP パートで辺に関する処理も行う場合)
//
// verified:
// Yosupo Library Checker - Tree Path Composite Sum
// https://judge.yosupo.jp/problem/tree_path_composite_sum
//
// AtCoder ABC 222 F - Expensive Expense
// https://atcoder.jp/contests/abc222/tasks/abc222_f
//
#include <bits/stdc++.h>
using namespace std;
// Edge Class
template<class T = long long> struct Edge {
int from, to;
T val;
Edge() : from(-1), to(-1) { }
Edge(int f, int t, T v = 1) : from(f), to(t), val(v) {}
friend ostream& operator << (ostream& s, const Edge& e) {
return s << e.from << "->" << e.to << "(" << e.val << ")";
}
};
// graph class
template<class T = long long> struct Graph {
vector<vector<Edge<T>>> list;
vector<vector<Edge<T>>> reversed_list;
Graph(int n = 0) : list(n), reversed_list(n) { }
void init(int n = 0) {
list.assign(n, vector<Edge<T>>());
reversed_list.assign(n, vector<Edge<T>>());
}
Graph &operator = (const Graph &g) {
list = g.list, reversed_list = g.reversed_list;
return *this;
}
const vector<Edge<T>> &operator [] (int i) const { return list[i]; }
const vector<Edge<T>> &get_rev_edges(int i) const { return reversed_list[i]; }
const size_t size() const { return list.size(); }
const void clear() { list.clear(); }
const void resize(int n) { list.resize(n); }
void add_edge(int from, int to, T val = 1) {
list[from].push_back(Edge(from, to, val));
reversed_list[to].push_back(Edge(to, from, val));
}
void add_bidirected_edge(int from, int to, T val = 1) {
list[from].push_back(Edge(from, to, val));
list[to].push_back(Edge(to, from, val));
reversed_list[from].push_back(Edge(from, to, val));
reversed_list[to].push_back(Edge(to, from, val));
}
friend ostream &operator << (ostream &s, const Graph &G) {
s << endl;
for (int i = 0; i < G.size(); ++i) {
s << i << " -> ";
for (const auto &e : G[i]) s << e.to << " ";
s << endl;
}
return s;
}
};
// 辺に重みがある場合の全方位木 DP
/*
通常の木 DP において、頂点 v を根とする部分根付き木に関する再帰関数 rec(v) について、
1. res = IDENTITY
2. 頂点 v の各子頂点 v2 (その辺を e とする) に対して:res = MERGE(res, ADDEDGE(e, rec(v2)))
3. return ADDNODE(v, res)
というような更新を行うものとする。
このような木 DP を全方位木 DP へと拡張する。
*/
template<class Monoid, class Weight = long long> struct WeightedReRooting {
using AddEdgeFunc = function<Monoid(Edge<Weight>, Monoid)>;
using MergeFunc = function<Monoid(Monoid, Monoid)>;
using AddNodeFunc = function<Monoid(int, Monoid)>;
// core member
Graph<Weight> G; // input graph
Monoid IDENTITY;
AddEdgeFunc ADDEDGE;
MergeFunc MERGE;
AddNodeFunc ADDNODE;
// inner data
vector<vector<Monoid>> dp;
vector<unordered_map<int,int>> ids;
// constructor
WeightedReRooting() {}
WeightedReRooting(const Graph<Weight> &g,
const AddEdgeFunc &addedge,
const MergeFunc &merge, const AddNodeFunc &addnode,
const Monoid &identity) {
G = g;
IDENTITY = identity;
ADDEDGE = addedge;
MERGE = merge;
ADDNODE = addnode;
build();
}
// re-looting dp
Monoid rec(int v, int p) {
Monoid res = IDENTITY;
dp[v].assign(G[v].size(), IDENTITY);
for (int i = 0; i < G[v].size(); ++i) {
int v2 = G[v][i].to;
ids[v][v2] = i;
if (v2 == p) continue;
dp[v][i] = rec(v2, v);
res = MERGE(res, ADDEDGE(G[v][i], dp[v][i]));
}
return ADDNODE(v, res);
}
void rerec(int v, int p, Monoid pval) {
for (int i = 0; i < G[v].size(); ++i) {
int v2 = G[v][i].to;
if (v2 == p) {
dp[v][i] = pval;
continue;
}
}
vector<Monoid> left(G[v].size() + 1, IDENTITY);
vector<Monoid> right(G[v].size() + 1, IDENTITY);
for (int i = 0; i < G[v].size(); ++i) {
int ri = (int)G[v].size() - i - 1;
left[i + 1] = MERGE(left[i], ADDEDGE(G[v][i], dp[v][i]));
right[i + 1] = MERGE(right[i], ADDEDGE(G[v][ri], dp[v][ri]));
}
for (int i = 0; i < G[v].size(); ++i) {
int v2 = G[v][i].to, ri = (int)G[v].size() - i - 1;
if (v2 == p) continue;
Monoid pval2 = MERGE(left[i], right[ri]);
rerec(v2, v, ADDNODE(v, pval2));
}
}
void build() {
dp.assign(G.size(), vector<Monoid>());
ids.assign(G.size(), unordered_map<int,int>());
int root = 0;
rec(root, -1);
rerec(root, -1, IDENTITY);
}
// getter
Monoid get(int v) {
Monoid res = IDENTITY;
for (int i = 0; i < G[v].size(); ++i) {
res = MERGE(res, ADDEDGE(G[v][i], dp[v][i]));
}
return ADDNODE(v, res);
}
Monoid get(int v, int w) {
return dp[v][ids[v][w]];
}
// dump
friend constexpr ostream& operator << (ostream &os, const WeightedReRooting &rr) {
for (int v = 0; v < rr.G.size(); ++v) {
for (int i = 0; i < rr.G[v].size(); ++i) {
os << rr.G[v][i] << ": " << rr.dp[v][i] << endl;
}
}
return os;
}
};
//------------------------------//
// Examples
//------------------------------//
// AtCoder ABC 222 F - Expensive Expense
void ABC_222_F() {
long long N, INF = 1LL << 62;
cin >> N;
Graph<long long> G(N);
for (int i = 0; i < N - 1; i++) {
long long a, b, c; cin >> a >> b >> c, a--, b--;
G.add_bidirected_edge(a, b, c);
}
vector<long long> D(N);
for (int i = 0; i < N; i++) cin >> D[i];
auto addedge = [&](Edge<long long> e, long long a) -> long long {
return a + e.val;
};
auto merge = [&](long long a, long long b) -> long long {
return max(a, b);
};
auto addnode = [&](int v, long long a) -> long long {
return max(a, D[v]);
};
WeightedReRooting<long long, long long> rr(G, addedge, merge, addnode, 0);
for (int v = 0; v < N; v++) {
long long res = 0;
for (auto e : G[v]) res = max(res, rr.get(v, e.to) + e.val);
cout << res << endl;
}
}
// Yosupo Library Checker - Tree Path Composite Sum
template<int MOD = 998244353, bool PRIME = true> struct Fp {
// inner value
unsigned int val;
// constructor
constexpr Fp() : val(0) { }
template<std::signed_integral T> constexpr Fp(T v) {
long long tmp = (long long)(v % (long long)(get_umod()));
if (tmp < 0) tmp += get_umod();
val = (unsigned int)(tmp);
}
template<std::unsigned_integral T> constexpr Fp(T v) {
val = (unsigned int)(v % get_umod());
}
constexpr long long get() const { return val; }
constexpr static int get_mod() { return MOD; }
constexpr static unsigned int get_umod() { return MOD; }
// arithmetic operators
constexpr Fp operator + () const { return Fp(*this); }
constexpr Fp operator - () const { return Fp() - Fp(*this); }
constexpr Fp operator + (const Fp &r) const { return Fp(*this) += r; }
constexpr Fp operator - (const Fp &r) const { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp &r) const { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp &r) const { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp &r) {
val += r.val;
if (val >= get_umod()) val -= get_umod();
return *this;
}
constexpr Fp& operator -= (const Fp &r) {
val -= r.val;
if (val >= get_umod()) val += get_umod();
return *this;
}
constexpr Fp& operator *= (const Fp &r) {
unsigned long long tmp = val;
tmp *= r.val;
val = (unsigned int)(tmp % get_umod());
return *this;
}
constexpr Fp pow(long long n) const {
assert(n >= 0);
Fp res(1), mul(*this);
while (n) {
if (n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
// other operators
constexpr bool operator == (const Fp &r) const {
return this->val == r.val;
}
constexpr bool operator != (const Fp &r) const {
return this->val != r.val;
}
constexpr bool operator < (const Fp &r) const {
return this->val < r.val;
}
constexpr bool operator > (const Fp &r) const {
return this->val > r.val;
}
constexpr bool operator <= (const Fp &r) const {
return this->val <= r.val;
}
constexpr bool operator >= (const Fp &r) const {
return this->val >= r.val;
}
constexpr Fp& operator ++ () {
++val;
if (val == get_umod()) val = 0;
return *this;
}
constexpr Fp& operator -- () {
if (val == 0) val = get_umod();
--val;
return *this;
}
constexpr Fp operator ++ (int) {
Fp res = *this;
++*this;
return res;
}
constexpr Fp operator -- (int) {
Fp res = *this;
--*this;
return res;
}
friend constexpr istream& operator >> (istream &is, Fp<MOD> &x) {
long long tmp = 1;
is >> tmp;
tmp = tmp % (long long)(get_umod());
if (tmp < 0) tmp += get_umod();
x.val = (unsigned int)(tmp);
return is;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD> &x) {
return os << x.val;
}
friend constexpr Fp<MOD> pow(const Fp<MOD> &r, long long n) {
return r.pow(n);
}
};
void YosupoTreePathCompositeSum() {
using mint = Fp<>;
using Weight = pair<mint,mint>;
using Monoid = pair<mint,int>;
int N;
cin >> N;
vector<mint> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
Graph<Weight> G(N);
for (int i = 0; i < N-1; i++) {
long long u, v;
mint b, c;
cin >> u >> v >> b >> c;
G.add_bidirected_edge(u, v, make_pair(b, c));
}
auto addedge = [&](const Edge<Weight> &e, Monoid a) -> Monoid {
return Monoid(e.val.first * a.first + e.val.second * a.second, a.second);
};
auto merge = [&](Monoid a, Monoid b) -> Monoid {
return Monoid(a.first + b.first, a.second + b.second);
};
auto addnode = [&](int v, Monoid a) -> Monoid {
return Monoid(a.first + A[v], a.second + 1);
};
WeightedReRooting<Monoid, Weight> rr(G, addedge, merge, addnode, make_pair(0, 0));
for (int v = 0; v < N; v++) cout << rr.get(v).first << " ";
cout << endl;
}
int main() {
//ABC_222_F();
YosupoTreePathCompositeSum();
}