mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 03:11:41 +08:00
27 lines
540 B
Go
27 lines
540 B
Go
package leetcode
|
|
|
|
func eventualSafeNodes(graph [][]int) []int {
|
|
res, color := []int{}, make([]int, len(graph))
|
|
for i := range graph {
|
|
if dfsEventualSafeNodes(graph, i, color) {
|
|
res = append(res, i)
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
// colors: WHITE 0, GRAY 1, BLACK 2;
|
|
func dfsEventualSafeNodes(graph [][]int, idx int, color []int) bool {
|
|
if color[idx] > 0 {
|
|
return color[idx] == 2
|
|
}
|
|
color[idx] = 1
|
|
for i := range graph[idx] {
|
|
if !dfsEventualSafeNodes(graph, graph[idx][i], color) {
|
|
return false
|
|
}
|
|
}
|
|
color[idx] = 2
|
|
return true
|
|
}
|