Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

CodeForces - 741D