CHOCOLA - Is this a question of trees?

https://www.spoj.com/problems/CHOCOLA/ this problem is in Trees category of A2OJ, can someone suggest me a tree solution to this problem?