How to solve problem F - 2Nodes in the JCPC 2019 contest?

Let's solve the problem if the path must pass through node u.
we'll assume node u has more than one child.

Add all paths starting from u to a set. Bruteforce a path by iterating the subtrees connected to u. Remove the subtree currently in form the set. Bruteforce each path from the subtree and remove the path in the set that is made up of nodes in the current path. then maximize the answer. This solution is $n^2log(n)$ because you need to repeat the process for every node, to make it faster use centroid decomposition. 

How to solve problem J ? 

Let freq[a] = the frequency of a in the array.
Let sum[a] = the sum of yi such that xi = a.
Then for all a  sum[a] >= freq[a] and there exists at least one a such that freq[a] = sum[a].
There are other conditions. Let me know if you need more help.