leetcode802 找到最终的安全状态

一:拓扑排序算法

 

class Solution {
    public List eventualSafeNodes(int[][] G) {
        int N=G.length;
        boolean[]safe=new boolean[N];//记录原图中没有出度的顶点
        List>graph=new ArrayList<>();
        List>rgraph=new ArrayList<>();//图的逆序,为了方便找到某一顶点的所有入边
        for(int i=0;i());
            rgraph.add(new HashSet<>());
        }

        Queuequeue=new LinkedList<>();
        for(int i=0;iv的边,如果u的出度为0,将其加入队列,依次类推
                if(graph.get(k).isEmpty()){
                    queue.offer(k);
                }
            }
        }
       Listans=new ArrayList<>();
        for(int i=0;i

 

 

你可能感兴趣的:(leetcode)