Bear and Forgotten Tree 3

CodeForces - 639B