Tree Painting
Contest: Educational Codeforces Round 67 (Rated for Div. 2)
Difficulty rating: 2100
Tags: dfs and similar, dp, trees

You are given a tree (an undirected connected acyclic graph) consisting of $$$n$$$ vertices. You are playing a game on this tree.

Initially all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn you choose a white vertex adjacent (connected by an edge) to any black vertex and paint it black.

Each time when you choose a vertex (even during the first turn), you gain the number of points equal to the size of the connected component consisting only of white vertices that contains the chosen vertex. The game ends when all vertices are painted black.

Let's see the following example:

Vertices $$$1$$$ and $$$4$$$ are painted black already. If you choose the vertex $$$2$$$, you will gain $$$4$$$ points for the connected component consisting of vertices $$$2, 3, 5$$$ and $$$6$$$. If you choose the vertex $$$9$$$, you will gain $$$3$$$ points for the connected component consisting of vertices $$$7, 8$$$ and $$$9$$$.

Your task is to maximize the number of points you gain.

Input format:
The first line contains an integer $$$n$$$ — the number of vertices in the tree ($$$2 \le n \le 2 \cdot 10^5$$$).

Each of the next $$$n - 1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by two integers $$$u_i$$$ and $$$v_i$$$, the indices of vertices it connects ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$).

It is guaranteed that the given edges form a tree.

Output format:
Print one integer — the maximum number of points you gain if you will play optimally.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
The first example tree is shown in the problem statement.

Example input:
9
1 2
2 3
2 5
2 6
1 4
4 9
9 7
9 8

Example output:
36

Example input:
5
1 2
1 3
2 4
2 5

Example output:
14

Editorial:
I should notice that there is much simpler idea and solution for this problem without rerooting technique but I will try to explain rerooting as the main solution of this problem (it can be applied in many problems and this is just very simple example).
What if the root of the tree is fixed? Then we can notice that the answer for a subtree can be calculated as $$$dp_v = s_v + \sum\limits_{to \in ch(v)} dp_{to}$$$, where $$$ch(v)$$$ is the set of children of the vertex $$$v$$$. The answer on the problem for the fixed root will be $$$dp_{root}$$$.
How can we calculate all possible values of $$$dp_{root}$$$ for each root from $$$1$$$ to $$$n$$$ fast enough? We can apply rerooting! When we change the root of tree from the vertex $$$v$$$ to the vertex $$$to$$$, we can notice that only four values will change: $$$s_v, s_{to}, dp_v$$$ and $$$dp_{to}$$$. Firstly, we need to cut the subtree of $$$to$$$ from the tree rooted at $$$v$$$. Let's subtract $$$dp_{to}$$$ and $$$s_{to}$$$ from $$$dp_v$$$, then let's change the size of the subtree of $$$v$$$ (subtract $$$s_{to}$$$ from it). Now we have the tree without the subtree of $$$to$$$. Then we need to append $$$v$$$ as a child of $$$to$$$. Add $$$s_v$$$ to $$$s_{to}$$$ and add $$$s_v$$$ and $$$dp_v$$$ to $$$dp_{to}$$$. Now we have $$$to$$$ as a root of the tree and can update the answer with $$$dp_{to}$$$. When we changes the root of the tree back from $$$to$$$ to $$$v$$$, we just need to rollback all changes we made.
So, overall idea is the following: calculate sizes of subtrees for some fixed root, calculate dynamic programming for this root, run dfs which will reroot the tree with any possible vertex and update the answer with the value of dynamic programming for each possible root.
The code of function that reroots the tree seems like this: