Time Complexity:
For more information about centroids and how to find/use them, see their module.
MAX_N = 200000g = [[] for i in range(MAX_N + 1)] # graphsubtree_size = [0 for i in range(MAX_N + 1)] # size of subtreedef dfs_size(curr: int, parent: int) -> None:"""calculates all subtree sizes (sum of child sizes + 1)"""subtree_size[curr] = 1for child in g[curr]:if child != parent:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!