Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Consider if the graph was only one cycle, what will be the answer for each node?
The actual graph will always consist of at least one cycle and some other nodes.
Calculate the answer for nodes in cycles the same way as in hint 1. How do you calculate the answer for the remaining nodes?