Tip 1: You can create the graph where nodes are cows and there is an edge for all pairs of cows whose weight is the squared distance between those cows. The problem then becomes finding a weight X such that G_X is connected where G_X is G minus all edges of weight > X. Tip 2: Compare G_X, G_{X-1} and a minimum spanning tree T of G for X is the solution. Tip 3: G_{X-1} is not connected, therefore is cannot contain all edges of T and thus T has a an edge of weight at least X. Now let us suppose that T is not included in G_X, T therefore contains an edge (a,b) of weight w. We can remove that edge from T which disconnect T into two tree T1 and T2 but we know that G_X is connected therefore there must be an edge (a',b') from one node of T1 to a node in T2 of weight ≤ X which means that T was not minimal. Tip 4: Build a MST of G and output the maximal weight of an used edge.