package main import fmt import github com skorobogatov input type Vert

  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
152
153
154
155
156
package main
import "fmt"
import "github.com/skorobogatov/input"
type Vertex struct {
bucket, list []int
sdom, dom, label, ancestor, parent int
}
var spike map[int]int
var count int
func BuildTree(CoT *([]Vertex), graph *([][]int), order map[int]int, num int) {
var prom Vertex
var k int
spike[num] = count
count++
prom.list = make([]int, 0)
prom.bucket = make([]int, 0)
prom.parent = 0
*CoT = append(*CoT, prom)
for i := 0; i < len((*graph)[num]); i++ {
if (*graph)[num][i] < 0 {
(*graph)[num][i] = order[-(*graph)[num][i]]-1
}
k = (*graph)[num][i]
if (spike[k] == 0) && (k < len(*graph)) {
BuildTree(CoT, graph, order, k)
}
}
for i := 0; i < len((*graph)[num]); i++ {
if (spike[(*graph)[num][i]] > 0) && ((*graph)[num][i] != num) {
(*CoT)[spike[(*graph)[num][i]]-1].list = append((*CoT)[spike[(*graph)[num][i]]-1].list, spike[num]-1)
if (spike[num] < spike[(*graph)[num][i]]) && ((*CoT)[spike[(*graph)[num][i]]-1].parent == 0) {
(*CoT)[spike[(*graph)[num][i]]-1].parent = spike[num]-1
}
}
}
}
func FindMin(CoT *([]Vertex), v int) int {
lens, ver := 0, 0
if (*CoT)[v].ancestor == -1 {
return v
}
stack := make([]int, len(*CoT))
u := v
for (*CoT)[(*CoT)[u].ancestor].ancestor != -1 {
stack[lens] = u
lens++
u = (*CoT)[u].ancestor
}
for lens != 0 {
lens--
ver = stack[lens]
if (*CoT)[(*CoT)[(*CoT)[ver].ancestor].label].sdom < (*CoT)[(*CoT)[ver].label].sdom {
(*CoT)[ver].label = (*CoT)[(*CoT)[ver].ancestor].label
}
(*CoT)[ver].ancestor = (*CoT)[u].ancestor
}
return (*CoT)[v].label
}
func Dominators(CoT *([]Vertex)) {
var prom, need int
for i := 0; i < len(*CoT); i++ {
(*CoT)[i].sdom, (*CoT)[i].label, (*CoT)[i].ancestor = i, i, -1
}
for i := len(*CoT)-1; i > 0; i-- {
for j := 0; j < len((*CoT)[i].list); j++ {
prom = FindMin(CoT, (*CoT)[i].list[j])
if (*CoT)[prom].sdom < (*CoT)[i].sdom {
(*CoT)[i].sdom = (*CoT)[prom].sdom
}
}
(*CoT)[i].ancestor = (*CoT)[i].parent
(*CoT)[(*CoT)[i].sdom].bucket = append((*CoT)[(*CoT)[i].sdom].bucket, i)
for j := 0; j < len((*CoT)[(*CoT)[i].parent].bucket); j++ {
need = (*CoT)[(*CoT)[i].parent].bucket[j]
prom = FindMin(CoT, need)
// fmt.Printf("fdgh\n")
if (*CoT)[prom].sdom == (*CoT)[need].sdom {
(*CoT)[need].dom = (*CoT)[i].parent
} else {
(*CoT)[need].dom = prom
}
}
//fmt.Printf("%d!!\n", (*CoT)[(*CoT)[i].parent].bucket)
(*CoT)[(*CoT)[i].parent].bucket = make([]int, 0)
}
for i := 1; i < len(*CoT); i++ {
if (*CoT)[i].dom != (*CoT)[i].sdom {
// fmt.Printf("drgxtgt\n")
(*CoT)[i].dom = (*CoT)[(*CoT)[i].dom].dom
}
}
(*CoT)[0].dom = -1;
}
func FindCycles(CoT []Vertex) int {
res := 0
control := make([]int, len(CoT))
for i := 0; i < len(CoT); i++ {
for j := 0; (j < len(CoT[i].list)) && (control[i] != -1); j++ {
for k := CoT[CoT[i].list[j]].dom; k != -1; k = CoT[k].dom {
if (k == i) {
res++
control[i] = -1
}
}
}
}
return res
}
func main() {
var n, label int
var command string
input.Scanf("%d", &n)
order := make(map[int]int, n)
graph := make([][]int, n)
spike = make(map[int]int, n)
for i := 0; i < n; i++ {
graph[i] = make([]int, 0)
input.Scanf("%d %s", &label, &command)
order[label] = i+1;
switch command {
case "ACTION": {
graph[i] = append(graph[i], i+1)
}
case "BRANCH": {
input.Scanf("%d", &label)
graph[i] = append(graph[i], i+1)
graph[i] = append(graph[i], -label)
}
case "JUMP": {
input.Scanf("%d", &label)
graph[i] = append(graph[i], -label)
}
}
}
CoT := make([]Vertex, 0)
count = 1
BuildTree(&CoT, &graph, order, 0)
/* for i := 0; i < count-1; i++ {
fmt.Printf("%d ", spike[i])
}
fmt.Printf("\n")
for i := 0; i < len(CoT); i++ {
for j := 0; j < len(CoT[i].list); j++ {
fmt.Printf("%d ", CoT[i].list[j])
}
fmt.Printf("\n")
}*/
Dominators(&CoT)
res := FindCycles(CoT)
/* for i := 0; i < len(CoT); i++ {
fmt.Printf("%d ", CoT[i].dom)
}
fmt.Printf("\n")
*/fmt.Printf("%d\n", res)
}