Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
The intended solution uses Disjoint Set Union.
Notice that, if <code>u</code> and <code>v</code> are not connected then the answer is <code>-1</code>, otherwise we can use all the edges from the connected component where both belong to.