func tarjan(arcs [][]int) (sccs [][]int) { var ( n = len(arcs) label = 0 status = make([]int, n) elts = make([]int, n) lo = 0 hi = n - 1 ) var dfs func(from int) int dfs = func(from int) int { min := label label = min + 1 status[from] = min elts[hi] = from hi-- for _, to := range arcs[from] { update := status[to] switch update { case -1: continue case 0: update = dfs(to) } if min > update { min = update } } if min == status[from] { for was := lo; ; { hi++ elt := elts[hi] elts[lo] = elt lo++ status[elt] = -1 if elt == from { scc := elts[was:lo] sccs = append(sccs, scc) break } } } return min } for from := range status { if status[from] == 0 { dfs(from) } } return }
To receive a hint, submit unfixed code.