Both DFS passes visit each node exactly once. The down pass computes size[v] and down[v] for all nodes. The up pass computes answer[v] for all nodes. Each pass is time.
Total time: for building the adjacency list, for the down pass, for the up pass. That gives overall.
Space: for the adjacency list, for the , , and arrays. Total space is .
Compare this to the brute force approach. For , rerooting finishes in milliseconds while brute force would time out.