LeetCode 1319 Number of Operations to Make Network Connected - Problem Statement

The problem

Given n computers and a list of cable connections, return the minimum number of cable moves to connect all computers.

You can disconnect a cable between two connected computers and reconnect it elsewhere.

With n = 4 and connections = [[0,1],[0,2],[1,2]]:

  • Computers 0, 1, 2 are connected. Computer 3 is isolated.
  • The 0-1-2 triangle has an extra cable (only need 22 cables for 33 computers).
  • Move the redundant cable to connect computer 3.
  • Answer: 11 operation.

Return 1-1 if impossible (not enough cables).

Constraints: 1n1051 \le n \le 10^5.