Graph Theory37 sections · 1633 units
Open in Course

Rerooting in Contests (Recognition)

(For each node signal)

Rerooting problems often say "for each node" or "for all possible roots" in the problem statement. That is your signal to think about rerooting. If brute force is O(n2)O(n^2) and constraints are n100,000n \leq 100{,}000 or n200,000n \leq 200{,}000, you need O(n)O(n) or O(nlogn)O(n \log n). Rerooting gives you O(n)O(n), which is often the intended solution.

After solving a few rerooting problems, you will see the pattern immediately. The hard part is setting up the DP state and combine function correctly, not the two-pass structure itself. That stays the same across all problems.

Space complexity is O(n)O(n) for the data structures used.