-
Notifications
You must be signed in to change notification settings - Fork 27
/
10305.go
56 lines (49 loc) · 907 Bytes
/
10305.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
// UVa 10305 - Ordering Tasks
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
var (
answer []string
visited map[int]bool
)
func dfs(links map[int][]int, n int) {
adjs := links[n]
for _, v := range adjs {
if !visited[v] {
dfs(links, v)
}
}
visited[n] = true
answer = append([]string{strconv.Itoa(n)}, answer...)
}
func topoSort(links map[int][]int, n int) {
visited = make(map[int]bool)
for i := 1; i <= n; i++ {
if !visited[i] {
dfs(links, i)
}
}
}
func main() {
in, _ := os.Open("10305.in")
defer in.Close()
out, _ := os.Create("10305.out")
defer out.Close()
var n, m, n1, n2 int
for {
if fmt.Fscanf(in, "%d%d", &n, &m); n == 0 && m == 0 {
break
}
links := make(map[int][]int)
for ; m > 0; m-- {
fmt.Fscanf(in, "%d%d", &n1, &n2)
links[n1] = append(links[n1], n2)
}
topoSort(links, n)
fmt.Fprintln(out, strings.Join(answer, " "))
}
}