• + 1 comment

    Couple of hints (based on my solution):

    Use disjoint sets to build a forest of trees.

    Process tree sizes in descending order (bigger trees will give a better maximum if processed first)

    Consider the amount of handshakes needed to build each tree and the final friendship count of the tree (which will be added each succesive handshake which DOESN'T involve this tree).

    Managed to solve it in Python 3 (my "standard fare" is C++) :)

    The complexity of the solution is, I believe, O(m+log q + q) where q is the quantity of trees which were built (usually significantly smaller than m -> worst case q==m)