-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode1489.go
86 lines (74 loc) · 1.54 KB
/
LeetCode1489.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"math"
"sort"
)
type unionFind struct {
parent []int
size []int
setCount int
}
func newUnionFind(n int) *unionFind {
parent := make([]int, n)
size := make([]int, n)
for i := range parent {
parent[i] = i
size[i] = 1
}
return &unionFind{parent: parent, size: size, setCount: n}
}
func (uf *unionFind) find(x int) int {
if uf.parent[x] != x {
uf.parent[x] = uf.find(uf.parent[x])
}
return uf.parent[x]
}
func (uf *unionFind) union(x, y int) bool {
fx, fy := uf.find(x), uf.find(y)
if fx == fy {
return false
}
if uf.size[fx] < uf.size[fy] {
fx, fy = fy, fx
}
uf.size[fx] += uf.size[fy]
uf.parent[fy] = fx
uf.setCount--
return true
}
func findCriticalAndPseudoCriticalEdges(n int, edges [][]int) [][]int {
for i , e := range edges {
edges[i] = append(e , i)
}
sort.Slice(edges , func(i, j int) bool {
return edges[i][2] < edges[j][2]
})
calcMst := func(uf *unionFind , ignoreID int) int {
mstValue := 0
for i , e := range edges {
if i != ignoreID && uf.union(e[0] , e[1]) {
mstValue += e[2]
}
}
if uf.setCount > 1 {
return math.MaxInt64
}
return mstValue
}
mstValue := calcMst(newUnionFind(n) , -1)
var keyEdges , pseudokeyEdges []int
for i , e := range edges {
if calcMst(newUnionFind(n) , i) > mstValue {
keyEdges = append(keyEdges , e[3])
continue
}
uf := newUnionFind(n)
uf.union(e[0] , e[1])
if e[2] + calcMst(uf , i ) == mstValue {
pseudokeyEdges = append(pseudokeyEdges , e[3])
}
}
return [][]int{keyEdges , pseudokeyEdges}
}
func main() {
}