Official

E - Through Path Editorial by en_translator


Each query asks to “add \(x\) to either connected component when the tree is cut by edge \(e\).”

Take an arbitrary root, and consider it as a rooted tree. Then, each query is equivalent to either “add \(x\) to descendants of a vertex” or “add \(x\) to vertices that is not descendants of a vertex.”

“Adding \(x\) to vertices that is not descendants of a vertex” is equivalent to “adding \(x\) to descendants of root” and then “adding \(-x\) to descendants of the vertex,” so every query boils down to the form of “add \(x\) to descendants of some vertex.”

Such queries can be processed in a total of \(O(N+W)\) time by computing the cumulative sums from the root to descendants, once at the end.

Also, we can observe that each value \(c_{a_i}-c_{b_i}\) is not affected by each query except for edge \(e\) itself, so the problem can also be solved similarly by managing \(c_1\) and \(c_{a_i}-c_{b_i}\).

Sample Code (C++) : https://atcoder.jp/contests/abc187/submissions/19103187
Sample Code (Python) : https://atcoder.jp/contests/abc187/submissions/19103203

posted:
last update: