-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask.go
160 lines (146 loc) · 3.4 KB
/
task.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
package main
// TODO переделать
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
)
type Node struct {
Value int
PointsTo AdjacencyList
}
type AdjacencyList []*Node
type Queue struct {
CurrentSize int
Queue AdjacencyList
FirstInQueue int
EmptyIndex int
}
func (q *Queue) Push(node *Node) {
q.Queue[q.EmptyIndex] = node
q.EmptyIndex++
q.EmptyIndex %= q.CurrentSize
predictedIndex := (q.EmptyIndex + 1) % q.CurrentSize
if q.FirstInQueue == predictedIndex {
q.Rebalance()
}
}
func (q *Queue) Pop() (*Node, error) {
if q.FirstInQueue == q.EmptyIndex {
return nil, errors.New("the queue is empty")
}
// тут я бы использовал defer, но это довольно нагруженная часть при ребалансировании, так что без него
save := q.Queue[q.FirstInQueue]
q.FirstInQueue++
q.FirstInQueue %= q.CurrentSize
return save, nil
}
func (q *Queue) Rebalance() {
newQueue := make(AdjacencyList, q.CurrentSize*2)
newQueueIndex := 0
for i, err := q.Pop(); err == nil; i, err = q.Pop() {
newQueue[newQueueIndex] = i
newQueueIndex++
}
q.Queue = newQueue
q.CurrentSize *= 2
q.FirstInQueue = 0
q.EmptyIndex = newQueueIndex
}
func Solution(r io.Reader, s *strings.Builder) {
scanner := bufio.NewScanner(r)
scanner.Scan()
initData := strings.Fields(scanner.Text())
peaks, _ := strconv.Atoi(initData[0])
edges, _ := strconv.Atoi(initData[1])
AL := make(AdjacencyList, peaks+1)
for i := 0; i < peaks; i++ {
currentIndex := i + 1
AL[currentIndex] = &Node{
Value: currentIndex,
PointsTo: nil,
}
}
for i := 0; i < edges; i++ {
scanner.Scan()
edgeData := strings.Fields(scanner.Text())
peakA, _ := strconv.Atoi(edgeData[0])
peakB, _ := strconv.Atoi(edgeData[1])
AL[peakA].PointsTo = append(AL[peakA].PointsTo, AL[peakB])
AL[peakB].PointsTo = append(AL[peakB].PointsTo, AL[peakA])
}
scanner.Scan()
N, _ := strconv.Atoi(scanner.Text())
queue := Queue{
CurrentSize: 16,
Queue: make(AdjacencyList, 16),
FirstInQueue: 0,
EmptyIndex: 0,
}
queue.Push(AL[N])
colors := make([]string, peaks+1)
colors[AL[N].Value] = "gray"
bfs(&queue, colors, s)
}
func bfs(q *Queue, colors []string, writer io.StringWriter) {
node, err := q.Pop()
if err != nil {
return
}
node.PointsTo = merge(node.PointsTo)
_, err = writer.WriteString(strconv.Itoa(node.Value) + " ")
if err != nil {
return
}
for _, n := range node.PointsTo {
if colors[n.Value] == "" {
colors[n.Value] = "gray"
q.Push(n)
}
}
colors[node.Value] = "black"
bfs(q, colors, writer)
}
func merge(list AdjacencyList) AdjacencyList {
listLength := len(list)
if listLength < 2 {
return list
}
mid := listLength >> 1
aList := merge(list[:mid])
aLength := len(aList)
bList := merge(list[mid:])
bLength := len(bList)
aIndex := 0
bIndex := 0
result := make(AdjacencyList, 0, aLength+bLength)
for aIndex < aLength || bIndex < bLength {
if aIndex == aLength {
result = append(result, bList[bIndex])
bIndex++
continue
}
if bIndex == bLength {
result = append(result, aList[aIndex])
aIndex++
continue
}
if aList[aIndex].Value < bList[bIndex].Value {
result = append(result, aList[aIndex])
aIndex++
} else {
result = append(result, bList[bIndex])
bIndex++
}
}
return result
}
func main() {
s := strings.Builder{}
Solution(os.Stdin, &s)
fmt.Println(strings.TrimSpace(s.String()))
}