publicclassSolution { public IList<int> EventualSafeNodes(int[][] graph) { int n = graph.Length; int[] color = newint[n]; IList<int> ans = new List<int>(); for (int i = 0; i < n; ++i) { if (Safe(graph, color, i)) { ans.Add(i); } } return ans; }
publicboolSafe(int[][] graph, int[] color, int x) { if (color[x] > 0) { return color[x] == 2; } color[x] = 1; foreach (int y in graph[x]) { if (!Safe(graph, color, y)) { returnfalse; } } color[x] = 2; returntrue; } }
var eventualSafeNodes = function(graph) { const n = graph.length; const color = newArray(n).fill(0); const ans = []; for (let i = 0; i < n; ++i) { if (safe(graph, color, i)) { ans.push(i); } } return ans; };
constsafe = (graph, color, x) => { if (color[x] > 0) { return color[x] === 2; } color[x] = 1; for (const y of graph[x]) { if (!safe(graph, color, y)) { returnfalse; } } color[x] = 2; returntrue; }
classSolution: defeventualSafeNodes(self, graph: List[List[int]]) -> List[int]: rg = [[] for _ in graph] for x, ys inenumerate(graph): for y in ys: rg[y].append(x) in_deg = [len(ys) for ys in graph]
q = deque([i for i, d inenumerate(in_deg) if d == 0]) while q: for x in rg[q.popleft()]: in_deg[x] -= 1 if in_deg[x] == 0: q.append(x)
classSolution { public: vector<int> eventualSafeNodes(vector<vector<int>> &graph){ int n = graph.size(); vector<vector<int>> rg(n); vector<int> inDeg(n); for (int x = 0; x < n; ++x) { for (int y : graph[x]) { rg[y].push_back(x); } inDeg[x] = graph[x].size(); }
queue<int> q; for (int i = 0; i < n; ++i) { if (inDeg[i] == 0) { q.push(i); } } while (!q.empty()) { int y = q.front(); q.pop(); for (int x : rg[y]) { if (--inDeg[x] == 0) { q.push(x); } } }
vector<int> ans; for (int i = 0; i < n; ++i) { if (inDeg[i] == 0) { ans.push_back(i); } } return ans; } };
publicclassSolution { public IList<int> EventualSafeNodes(int[][] graph) { int n = graph.Length; IList<IList<int>> rg = new List<IList<int>>(); for (int i = 0; i < n; ++i) { rg.Add(new List<int>()); } int[] inDeg = newint[n]; for (int x = 0; x < n; ++x) { foreach (int y in graph[x]) { rg[y].Add(x); } inDeg[x] = graph[x].Length; }
Queue<int> queue = new Queue<int>(); for (int i = 0; i < n; ++i) { if (inDeg[i] == 0) { queue.Enqueue(i); } } while (queue.Count > 0) { int y = queue.Dequeue(); foreach (int x in rg[y]) { if (--inDeg[x] == 0) { queue.Enqueue(x); } } }
IList<int> ans = new List<int>(); for (int i = 0; i < n; ++i) { if (inDeg[i] == 0) { ans.Add(i); } } return ans; } }
funceventualSafeNodes(graph [][]int) (ans []int) { n := len(graph) rg := make([][]int, n) inDeg := make([]int, n) for x, ys := range graph { for _, y := range ys { rg[y] = append(rg[y], x) } inDeg[x] = len(ys) }
q := []int{} for i, d := range inDeg { if d == 0 { q = append(q, i) } } forlen(q) > 0 { y := q[0] q = q[1:] for _, x := range rg[y] { inDeg[x]-- if inDeg[x] == 0 { q = append(q, x) } } }
for i, d := range inDeg { if d == 0 { ans = append(ans, i) } } return }
var eventualSafeNodes = function(graph) { const n = graph.length; const rg = newArray(n).fill(0).map(() =>newArray()); const inDeg = newArray(n).fill(0); for (let x = 0; x < n; ++x) { for (let y of graph[x]) { rg[y].push(x); } inDeg[x] = graph[x].length; }
const queue = []; for (let i = 0; i < n; ++i) { if (inDeg[i] === 0) { queue.push(i); } } while (queue.length) { const y = queue.shift(); for (const x of rg[y]) { if (--inDeg[x] === 0) { queue.push(x); } } }
const ans = []; for (let i = 0; i < n; ++i) { if (inDeg[i] === 0) { ans.push(i); } } return ans; };
int* eventualSafeNodes(int** graph, int graphSize, int* graphColSize, int* returnSize) { int n = graphSize; int* rg[n]; int rgCol[n]; memset(rgCol, 0, sizeof(rgCol)); for (int i = 0; i < n; i++) { for (int j = 0; j < graphColSize[i]; j++) { rgCol[graph[i][j]]++; } } for (int i = 0; i < n; i++) { rg[i] = malloc(sizeof(int) * rgCol[i]); rgCol[i] = 0; } for (int i = 0; i < n; i++) { for (int j = 0; j < graphColSize[i]; j++) { rg[graph[i][j]][rgCol[graph[i][j]]++] = i; } } int inDeg[n]; memcpy(inDeg, graphColSize, sizeof(int) * n);
int que[10000]; int left = 0, right = 0; for (int i = 0; i < n; ++i) { if (inDeg[i] == 0) { que[right++] = i; } } while (left < right) { int y = que[left++]; for (int i = 0; i < rgCol[y]; i++){ if (--inDeg[rg[y][i]] == 0) { que[right++] = rg[y][i]; } } }
int *ans = malloc(sizeof(int) * n); *returnSize = 0; for (int i = 0; i < n; ++i) { if (inDeg[i] == 0) { ans[(*returnSize)++] = i; } } return ans; }