-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcomponent-path.go
341 lines (273 loc) · 8.91 KB
/
component-path.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
Идея: Используем алгоритм Таржана для того, чтобы найти все сильно связанные компоненты
https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
Когда все articulation points найдены, мы можем раскрасить граф (каждую компоненту в свой цвет).
После этого, используя цвета, можно построить дерево, узлами которого будут являться бывшие связанные компоненты.
После этого не составит труда найти самый длинный путь в дереве, если отсортировать все вершины по убыванию и пытаться из их по одному ребру нарастить такой путь вниз (простейший вариант динамического программирования).
Итоговая сложность получится:
* Построение компонент: O(V + E) по времени и памяти
* Раскрашивание: O(V + E) по времени и памяти
* Сортировка для вывода весов и нахождения длиннейшего пути: O(V log) по времени
* Нахождение длиннейшего пути и вывод: O(V + E) по времени и памяти
Итого полная сложность выходит O(V log V + E) по времени, O(V + E) по памяти
*/
package main
import "fmt"
import "sort"
import "bufio"
import "os"
// Just helper methods and definitions
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
type IntArray []int
func (array IntArray) Len() int {
return len(array)
}
func (array IntArray) Less(i, j int) bool {
return array[i] < array[j]
}
func (array IntArray) Swap(i, j int) {
array[i], array[j] = array[j], array[i]
}
// Colors used during the DFS
// WHITE - not visited
// GRAY - in the recursion stack
// BLACK - all the children are visited, removed from the stack
type Color int64
const (
WHITE Color = iota
GRAY
BLACK
)
type Vertex struct {
label int // Position from the input
weight int // A number from the input
enterTimestamp int // Timestamp when we first seen the vertex
lowestReachableTimestamp int // Lowest enter timestamp reachable from here (by back edges)
dfsColor Color // Color used during dfs
adj Graph // A list of adjacent vertices
adjRemoved []bool // Indicated an edge has been temporarily removed
color int // Color of a biconnected component (each component has its own assigned)
}
type Graph []*Vertex
func (vertices Graph) Len() int {
return len(vertices)
}
func (vertices Graph) Less(i, j int) bool {
return vertices[i].weight < vertices[j].weight
}
func (vertices Graph) Swap(i, j int) {
vertices[i], vertices[j] = vertices[j], vertices[i]
}
func newVertex(label int, weight int) *Vertex {
// Helps create a vertex
return &Vertex{label, weight, 0, 0, WHITE, Graph{}, []bool{}, -1}
}
func find(array Graph, target *Vertex) int {
/* Finds the position of the target in the array */
toRemove := -1
for pos, val := range array {
if val.label == target.label {
toRemove = pos
break
}
}
return toRemove
}
func dfs(vertex *Vertex, timestamp int) int {
/*
Dfs that sets timestamps used in Tarjan's algorithm
to find articulation points
*/
vertex.dfsColor = GRAY
vertex.enterTimestamp = timestamp
vertex.lowestReachableTimestamp = timestamp
timestamp++
for nextPos, nextVertex := range vertex.adj {
if vertex.adjRemoved[nextPos] {
continue
}
if nextVertex.dfsColor == GRAY {
// We have a back edge
if vertex.lowestReachableTimestamp > nextVertex.enterTimestamp {
vertex.lowestReachableTimestamp = nextVertex.enterTimestamp
}
} else if nextVertex.dfsColor == WHITE {
// We have a new tree edge
toRemove := find(nextVertex.adj, vertex)
nextVertex.adjRemoved[toRemove] = true
timestamp = dfs(nextVertex, timestamp)
nextVertex.adjRemoved[toRemove] = false
if vertex.lowestReachableTimestamp > nextVertex.lowestReachableTimestamp {
vertex.lowestReachableTimestamp = nextVertex.lowestReachableTimestamp
}
}
}
vertex.dfsColor = BLACK
return timestamp
}
func paint(vertex *Vertex, color int, maxColor int) int {
/*
Color the graph with respect to the articulation points.
If we find a bridge which connects two biconnected
components, we don't follow such a bridge, so we will
color only current component
*/
vertex.dfsColor = GRAY
vertex.color = color
for _, adjVertex := range vertex.adj {
// Already processed
if adjVertex.dfsColor != WHITE {
continue
}
if adjVertex.lowestReachableTimestamp > vertex.enterTimestamp {
// This is an articulation point, should switch to
// the next color
tmp := paint(adjVertex, maxColor+1, maxColor+1)
if tmp > maxColor {
maxColor = tmp
}
} else {
// Not an articulation point, need to use the same color
tmp := paint(adjVertex, color, maxColor)
if tmp > maxColor {
maxColor = tmp
}
}
}
vertex.dfsColor = BLACK
return maxColor
}
func biconnectComponents(vertices Graph) {
/*
Find all biconnected components
*/
timestamp := 0
// Set dfs timestamps
for _, vertex := range vertices {
if vertex.dfsColor == WHITE {
timestamp = dfs(vertex, timestamp)
}
}
// Reset dfs timestamps
for _, vertex := range vertices {
vertex.dfsColor = WHITE
}
// Paint components
color := 0
for _, vertex := range vertices {
if vertex.color == -1 {
color = paint(vertex, color, color) + 1
}
}
}
func buildCompressedGraph(vertices Graph) Graph {
/*
Given a biconnected graph, build a condensed version of it
*/
colors := 0
for _, vertex := range vertices {
if vertex.color+1 > colors {
colors = vertex.color + 1
}
}
compressedVertices := make(Graph, colors)
for pos := 0; pos < colors; pos++ {
compressedVertices[pos] = newVertex(pos+1, 0)
compressedVertices[pos].color = pos
}
for _, vertex := range vertices {
compressedVertices[vertex.color].weight += vertex.weight
for _, adjVertex := range vertex.adj {
if vertex.color == adjVertex.color {
continue
}
compressedVertices[vertex.color].adj = append(compressedVertices[vertex.color].adj, compressedVertices[adjVertex.color])
}
}
return compressedVertices
}
func dfsDepth(vertex *Vertex, asc bool) int {
/*
Try to follow the graph in ascending (descending) weights order
and calculate the maximum depth reachable
*/
vertex.dfsColor = GRAY
maxDepth := 0
for _, adjVertex := range vertex.adj {
if asc && adjVertex.weight > vertex.weight {
depth := dfsDepth(adjVertex, asc)
if depth > maxDepth {
maxDepth = depth
}
}
if !asc && adjVertex.weight < vertex.weight {
depth := dfsDepth(adjVertex, asc)
if depth > maxDepth {
maxDepth = depth
}
}
}
vertex.dfsColor = BLACK
return maxDepth + 1
}
func longestPath(vertices Graph) int {
/*
Get the longest path which is strictly ascending (descending)
*/
paths := make([]int, len(vertices))
for pos, _ := range vertices {
paths[pos] = 1
}
for pos, _ := range vertices {
vertex := vertices[len(vertices)-pos-1]
for _, adjVertex := range vertex.adj {
if vertex.weight > adjVertex.weight {
if paths[vertex.label-1]+1 > paths[adjVertex.label-1] {
paths[adjVertex.label-1] = paths[vertex.label-1] + 1
}
}
}
}
maxPath := 1
for _, path := range paths {
if path > maxPath {
maxPath = path
}
}
return maxPath
}
func componentPath(vertices Graph) (Graph, int) {
biconnectComponents(vertices)
compressedVertices := buildCompressedGraph(vertices)
sort.Sort(compressedVertices)
return compressedVertices, longestPath(compressedVertices)
}
func main() {
defer writer.Flush()
var numVertices, numEdges int
scanf("%d %d\n", &numVertices, &numEdges)
vertices := make(Graph, numVertices)
for pos := 0; pos < numVertices; pos++ {
var weight int
scanf("%d", &weight)
vertices[pos] = newVertex(pos+1, weight)
}
scanf("\n")
for pos := 0; pos < numEdges; pos++ {
var src, dst int
scanf("%d %d\n", &src, &dst)
vertices[src-1].adj = append(vertices[src-1].adj, vertices[dst-1])
vertices[src-1].adjRemoved = append(vertices[src-1].adjRemoved, false)
vertices[dst-1].adj = append(vertices[dst-1].adj, vertices[src-1])
vertices[dst-1].adjRemoved = append(vertices[dst-1].adjRemoved, false)
}
compressedVertices, longestCompressedPath := componentPath(vertices)
for _, vertex := range compressedVertices {
printf("%d ", vertex.weight)
}
printf("\n")
printf("%d\n", longestCompressedPath)
}