mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
25 lines
505 B
Go
25 lines
505 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/template"
|
|
)
|
|
|
|
func makeConnected(n int, connections [][]int) int {
|
|
if n-1 > len(connections) {
|
|
return -1
|
|
}
|
|
uf, redundance := template.UnionFind{}, 0
|
|
uf.Init(n)
|
|
for _, connection := range connections {
|
|
if uf.Find(connection[0]) == uf.Find(connection[1]) {
|
|
redundance++
|
|
} else {
|
|
uf.Union(connection[0], connection[1])
|
|
}
|
|
}
|
|
if uf.TotalCount() == 1 || redundance < uf.TotalCount()-1 {
|
|
return 0
|
|
}
|
|
return uf.TotalCount() - 1
|
|
}
|