问:
原题链接:找到最终的安全状态
答:
class Solution {
public List<Integer> eventualSafeNodes(int[][] graph) {
int len = graph.length;
int[] color = new int[len];
List<Integer> list = new ArrayList<Integer>();
for(int i = 0;i < len;i++)
{
if(safe(graph,color,i))
list.add(i);
}
return list;
}
public boolean safe(int[][] graph,int[] color,int x){
if(color[x] > 0)
return color[x] == 2;
color[x] = 1;
for(int y:graph[x])
{
if(!safe(graph,color,y))
return false;
}
color[x] = 2;
return true;
}
}