-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphbase.go
More file actions
151 lines (134 loc) · 2.81 KB
/
graphbase.go
File metadata and controls
151 lines (134 loc) · 2.81 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
)
type Node struct {
index int
adj map[int]*Node
}
type Arrow struct {
u, v int
}
type Stack []*Node
func (s *Stack) Push(n *Node) {
*s = append(*s, n)
}
func (s *Stack) Pop() *Node {
l := len(*s)
n := (*s)[l-1]
*s = (*s)[:l-1]
return n
}
func tarjan(graph []*Node) (map[int]int, int) {
in := make(map[int]int)
low := make(map[int]int)
comp := make(map[int]int)
for _, n := range graph {
in[n.index], low[n.index], comp[n.index] = 0, 0, -1
}
stack := Stack(make([]*Node, 0))
time, count := 1, 0
var visit func(v *Node)
visit = func(v *Node) {
in[v.index], low[v.index] = time, time
time++
stack.Push(v)
for _, u := range v.adj {
if in[u.index] == 0 {
visit(u)
}
if comp[u.index] == -1 && low[v.index] > low[u.index] {
low[v.index] = low[u.index]
}
}
if in[v.index] == low[v.index] {
for {
u := stack.Pop()
comp[u.index] = count
if u.index == v.index {
break
}
}
count++
}
}
for _, v := range graph {
if in[v.index] == 0 {
visit(v)
}
}
return comp, count
}
func scanInt(scanner *bufio.Scanner) int {
scanner.Scan()
n, _ := strconv.Atoi(scanner.Text())
return n
}
func dfs(condensation []*Node) map[int]bool {
kernel := make(map[int]bool)
visited := make(map[int]bool)
var visit func(v *Node)
visit = func(v *Node) {
visited[v.index] = true
for _, u := range v.adj {
if !visited[u.index] {
visit(u)
}
delete(kernel, u.index)
}
}
for _, scc := range condensation {
if _, isVisited := visited[scc.index]; !isVisited {
visit(scc)
kernel[scc.index] = true
}
}
return kernel
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
n, m := scanInt(scanner), scanInt(scanner)
graph := make([]*Node, n)
for i := 0; i < n; i++ {
graph[i] = &Node{i, make(map[int]*Node, 0)}
}
arrows := make([]Arrow, m)
for i := 0; i < m; i++ {
u, v := scanInt(scanner), scanInt(scanner)
graph[u].adj[v] = graph[v]
arrows[i] = Arrow{u, v}
}
comp, count := tarjan(graph)
condensation := make([]*Node, count)
for i, _ := range condensation {
condensation[i] = &Node{index:i, adj:make(map[int]*Node, 0)}
}
for i, _ := range arrows {
if comp[arrows[i].u] == comp[arrows[i].v] {
continue
}
condensation[comp[arrows[i].u]].adj[comp[arrows[i].v]] = condensation[comp[arrows[i].v]]
}
kernel := dfs(condensation)
minCompIndex := make(map[int]int)
for _, v := range graph {
if _, ok := minCompIndex[comp[v.index]]; !ok {
minCompIndex[comp[v.index]] = v.index
}
}
answer := make([]int, 0, len(kernel))
for index, _ := range kernel {
answer = append(answer, minCompIndex[index])
}
sort.Ints(answer)
writer := bufio.NewWriter(os.Stdout)
for _, index := range answer {
fmt.Fprintf(writer, "%d ", index)
}
writer.Flush()
}