Run a DFS that assigns each node to a chain. Keep a counter that increments each time you start a new chain. At each node , if is the start of a chain (no heavy edge from parent, or is root), assign chain[v] = chain_id and increment .
Then recurse on the heavy child first with the same chain ID, then on light children with new chain IDs. This takes time and labels every node with its chain. The heavy child continues the parent's chain. Light children start new chains.
Space complexity is for the data structures used.