-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (168 loc) · 4.15 KB
/
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// Complete the balancedForest function below.
func balancedForest(c []int32, edges [][]int32) int64 {
neighbors := make([][]int32, len(c))
for _, e := range edges {
neighbors[e[0]-1] = append(neighbors[e[0]-1], e[1]-1)
neighbors[e[1]-1] = append(neighbors[e[1]-1], e[0]-1)
}
markers := make([]bool, len(c))
parents := make([]int32, len(c))
sizeMap := make(map[int64][]int32)
sizes := make([]int64, len(c))
var size int64
for _, s := range c {
size += int64(s)
}
var buildTree func(int32) int64
buildTree = func(nodeIndex int32) int64 {
markers[nodeIndex] = true
nodeSize := int64(c[nodeIndex])
for _, n := range neighbors[nodeIndex] {
if markers[n] {
continue
}
parents[n] = nodeIndex
nodeSize += buildTree(n)
}
sizes[nodeIndex] = nodeSize
if isValidSubTree(size, nodeSize) {
sizeMap[nodeSize] = append(sizeMap[nodeSize], nodeIndex)
}
return nodeSize
}
buildTree(0)
min := int64(-1)
getValidSpread := func(nodeSize int64, nodes []int32) int64 {
if isLargerSubTree(size, nodeSize) {
upper := nodeSize
lower := size - nodeSize*2
spread := upper - lower
if lower == 0 {
return spread
}
if len(nodes) >= 2 {
return spread
}
for _, n := range nodes {
if hasParentSize(n, parents, sizes, upper+lower) || hasParentSize(n, parents, sizes, upper+upper) {
return spread
}
if lower == upper {
break
}
for _, l := range sizeMap[lower] {
if !hasParentNode(l, parents, n) {
return spread
}
}
}
}
if isSmallerSubTree(size, nodeSize) {
lower := nodeSize
upper := (size - nodeSize) / 2
spread := upper - lower
for _, n := range sizeMap[nodeSize] {
if hasParentSize(n, parents, sizes, upper+lower) {
return spread
}
}
}
return -1
}
for nodeSize, nodes := range sizeMap {
spread := getValidSpread(nodeSize, nodes)
if min == -1 || (spread != -1 && spread < min) {
min = spread
}
}
return min
}
func hasParentNode(node int32, parents []int32, expectedParent int32) bool {
parent := parents[node]
for parent != 0 {
if parent == expectedParent {
return true
}
parent = parents[parent]
}
return false
}
func hasParentSize(node int32, parents []int32, sizes []int64, expectedSize int64) bool {
parent := parents[node]
for parent != 0 {
if sizes[parent] == expectedSize {
return true
}
parent = parents[parent]
}
return false
}
func isLargerSubTree(size, nodeSize int64) bool {
return nodeSize*2 <= size && size-nodeSize*2 <= nodeSize
}
func isSmallerSubTree(size, nodeSize int64) bool {
return nodeSize%2 == size%2 && (size-nodeSize)/2 >= nodeSize
}
func isValidSubTree(size, nodeSize int64) bool {
return isLargerSubTree(size, nodeSize) || isSmallerSubTree(size, nodeSize)
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 1024*1024)
stdout := os.Stdout
writer := bufio.NewWriterSize(stdout, 1024*1024)
qTemp, err := strconv.ParseInt(readLine(reader), 10, 64)
checkError(err)
q := int32(qTemp)
for qItr := 0; qItr < int(q); qItr++ {
nTemp, err := strconv.ParseInt(readLine(reader), 10, 64)
checkError(err)
n := int32(nTemp)
cTemp := strings.Split(readLine(reader), " ")
var c []int32
for i := 0; i < int(n); i++ {
cItemTemp, err := strconv.ParseInt(cTemp[i], 10, 64)
checkError(err)
cItem := int32(cItemTemp)
c = append(c, cItem)
}
var edges [][]int32
for i := 0; i < int(n)-1; i++ {
edgesRowTemp := strings.Split(readLine(reader), " ")
var edgesRow []int32
for _, edgesRowItem := range edgesRowTemp {
edgesItemTemp, err := strconv.ParseInt(edgesRowItem, 10, 64)
checkError(err)
edgesItem := int32(edgesItemTemp)
edgesRow = append(edgesRow, edgesItem)
}
if len(edgesRow) != 2 {
panic("Bad input")
}
edges = append(edges, edgesRow)
}
result := balancedForest(c, edges)
fmt.Fprintf(writer, "%d\n", result)
}
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}