-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask.go
85 lines (70 loc) · 1.39 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
type Node struct {
value int
to []*Node
}
type Stack struct {
cnt int
stack []string
}
func main() {
s := strings.Builder{}
Solution(os.Stdin, &s)
fmt.Println(s.String())
}
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])
list := make([]*Node, peaks+1)
for i := 1; i <= peaks; i++ {
list[i] = &Node{
value: i,
}
}
for i := 0; i < edges; i++ {
scanner.Scan()
data := strings.Fields(scanner.Text())
peakA, _ := strconv.Atoi(data[0])
peakB, _ := strconv.Atoi(data[1])
list[peakA].to = append(list[peakA].to, list[peakB])
}
colors := make([]string, peaks+1)
stack := Stack{
cnt: peaks - 1,
stack: make([]string, peaks),
}
for i := peaks; i > 0; i-- {
if colors[i] == "" {
Sort(list[i], colors, &stack)
}
}
s.WriteString(fmt.Sprint(&stack))
}
func (s *Stack) String() string {
return strings.Join(s.stack, " ")
}
func (s *Stack) Push(v string) {
s.stack[s.cnt] = v
s.cnt--
}
func Sort(n *Node, colors []string, stack *Stack) {
colors[n.value] = "gray"
for _, node := range n.to {
if colors[node.value] == "" {
Sort(node, colors, stack)
}
}
colors[n.value] = "black"
stack.Push(strconv.Itoa(n.value))
}