-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (42 loc) · 961 Bytes
/
main.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
package main
import (
"fmt"
)
func main() {
fmt.Println(maximumSubtreeSize([][]int{{0, 1}, {0, 2}, {0, 3}}, []int{1, 1, 2, 3}))
}
func maximumSubtreeSize(edges [][]int, colors []int) int {
var result int
graph := make([][]int, len(edges)+1)
for _, v := range edges {
graph[v[0]] = append(graph[v[0]], v[1])
graph[v[1]] = append(graph[v[1]], v[0])
}
visited := make([]bool, len(colors))
var runner func(graph [][]int, curr int) (int, bool)
runner = func(graph [][]int, curr int) (tmp int, ok bool) {
visited[curr] = true
currColor := colors[curr]
var check bool
for _, v := range graph[curr] {
if !visited[v] {
newSize, ok := runner(graph, v)
if colors[v] == currColor {
tmp += newSize
} else {
check = true
}
if !ok {
check = true
}
}
}
if !check {
result = max(result, tmp+1)
return tmp + 1, true
}
return 1, false
}
val, _ := runner(graph, 0)
return max(result, val)
}