-
Notifications
You must be signed in to change notification settings - Fork 481
/
1136.go
42 lines (39 loc) · 873 Bytes
/
1136.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
func minimumSemesters(N int, relations [][]int) int {
g := make([][]int, N)
for i := 0; i < N; i++ {
g[i] = []int{}
}
degrees := make([]int, N)
for _, v := range relations {
x, y := v[0] - 1, v[1] - 1
g[x] = append(g[x], y)
degrees[y]++
}
num, res := 0, 0
q := []int{}
for i := 0; i < N; i++ {
if degrees[i] == 0 {
q = append(q, i)
num++
}
}
for len(q) > 0 {
n := len(q)
for i := 0; i < n; i++ {
v := q[0]
q = q[1:]
for _, w := range g[v] {
degrees[w]--
if degrees[w] == 0 {
q = append(q, w)
num++
}
}
}
res++
}
if num == N {
return res
}
return -1
}