-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask.go
96 lines (76 loc) · 1.35 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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)
type NodeList []*Node
type Node struct {
value int
points []*Node
}
func main() {
s := strings.Builder{}
GetList(os.Stdin, &s)
fmt.Println(s.String())
}
func GetList(r io.Reader, s *strings.Builder) {
scanner := bufio.NewScanner(r)
scanner.Scan()
var n, m int
var err error
nValues := strings.Fields(scanner.Text())
n, err = strconv.Atoi(nValues[0])
if err != nil {
log.Fatalln(err)
}
m, err = strconv.Atoi(nValues[1])
if err != nil {
log.Fatalln(err)
}
list := make(NodeList, n+1)
for i := 0; i < m; i++ {
scanner.Scan()
edge := strings.Fields(scanner.Text())
var u, v int
u, err = strconv.Atoi(edge[0])
if err != nil {
log.Fatalln(err)
}
v, err = strconv.Atoi(edge[1])
if err != nil {
log.Fatalln(err)
}
if list[u] == nil {
list[u] = &Node{value: u}
}
if list[v] == nil {
list[v] = &Node{value: v}
}
list[u].points = append(list[u].points, list[v])
}
for i, v := range list {
if i == 0 {
continue
}
if v == nil {
s.WriteString("0\n")
continue
}
if len(v.points) == 0 {
s.WriteString("0\n")
continue
}
s.WriteString(strconv.Itoa(len(v.points)) + " ")
for _, g := range v.points {
s.WriteString(strconv.Itoa(g.value) + " ")
}
if i != len(list)-1 {
s.WriteByte('\n')
}
}
}