Tap here to switch tabs
Problem
Submissions

Problem: Number of Operations to Make Network Connected

med
30 min
Try to solve the Number of Operations to Make Network Connected problem.

Statement

You are given n computers labeled from 00 to n−1n - 1 and a list connections, where each element connections[i] = [a_i, b_i] represents a cable directly connecting computers a_i and b_i.

A cable can be removed from its current pair of computers and reconnected between any two computers.

Return the minimum number of such cable move operations needed to make all n computers part of a single connected network. If it is impossible to connect all computers, return −1-1.

Constraints:

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq connections.length ≤min⁡(‘n‘⋅(‘n‘−1)2,105)\leq \min\left(\frac{`n` \cdot (`n` - 1)}{2}, 10^5\right)

  • connections[i].length ==2== 2

  • 0≤0 \leq a_i, b_i << n

  • a_i ≠\neq b_i

  • There are no repeated connections.

  • No two computers are connected by more than one cable.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Number of Operations to Make Network Connected

med
30 min
Try to solve the Number of Operations to Make Network Connected problem.

Statement

You are given n computers labeled from 00 to n−1n - 1 and a list connections, where each element connections[i] = [a_i, b_i] represents a cable directly connecting computers a_i and b_i.

A cable can be removed from its current pair of computers and reconnected between any two computers.

Return the minimum number of such cable move operations needed to make all n computers part of a single connected network. If it is impossible to connect all computers, return −1-1.

Constraints:

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq connections.length ≤min⁡(‘n‘⋅(‘n‘−1)2,105)\leq \min\left(\frac{`n` \cdot (`n` - 1)}{2}, 10^5\right)

  • connections[i].length ==2== 2

  • 0≤0 \leq a_i, b_i << n

  • a_i ≠\neq b_i

  • There are no repeated connections.

  • No two computers are connected by more than one cable.