-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
task.go
90 lines (72 loc) · 1.48 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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)
const MOD = 1000000007
type Vertex struct {
value int
pointsTo []*Vertex
}
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)
var err error
var n, m int
var arrStr []string
scanner.Scan()
arrStr = strings.Fields(scanner.Text())
n, err = strconv.Atoi(arrStr[0])
if err != nil {
log.Fatal(err)
}
m, err = strconv.Atoi(arrStr[1])
if err != nil {
log.Fatal(err)
}
list := make([]Vertex, n+1)
for i := 1; i < n+1; i++ {
list[i].value = i
}
var a, b int
for i := 0; i < m; i++ {
scanner.Scan()
arrStr = strings.Fields(scanner.Text())
a, err = strconv.Atoi(arrStr[0])
if err != nil {
log.Fatal(err)
}
b, err = strconv.Atoi(arrStr[1])
if err != nil {
log.Fatal(err)
}
list[a].pointsTo = append(list[a].pointsTo, &list[b])
}
scanner.Scan()
arrStr = strings.Fields(scanner.Text())
from, _ := strconv.Atoi(arrStr[0])
to, _ := strconv.Atoi(arrStr[1])
paths := make([]int, n+1)
visited := make([]bool, n+1)
paths[to] = 1
dfs(&list[from], visited, paths)
s.WriteString(fmt.Sprint(paths[from]))
}
func dfs(vertex *Vertex, visited []bool, paths []int) {
visited[vertex.value] = true
for _, v := range vertex.pointsTo {
if !visited[v.value] {
dfs(v, visited, paths)
}
paths[vertex.value] = (paths[vertex.value] + paths[v.value]) % MOD
}
}