Here is your first rerooting problem. Given a tree of nodes, compute the sum of distances from each node to all other nodes. The distance between two nodes is the number of edges on the path between them.
Input: first line is . Next lines each have two integers and describing an edge. Output: integers, where the -th integer is the sum of distances from node to every other node.
Brute force runs a BFS from each node in . You need using rerooting. Think about what changes when you shift the root from to child .