Tree-String Problem

CodeForces - 291E