Comments

I also followed this approach, my submission: 375149035

i meant the case where S is defined to include all vertices of the tree, not just the leaves, this leads to a different problem

How to solve H if the set S contains all vertices of the tree?

This's the best achievement ive ever had, it’s a pity to hear that