package class16; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.Queue; // OJ链接:https://www.lintcode.com/problem/topological-sorting public class Code03_TopologicalOrderBFS1 { // 不要提交这个类 public static class DirectedGraphNode { public int label; public ArrayList neighbors; public DirectedGraphNode(int x) { label = x; neighbors = new ArrayList(); } } // 提交下面的 public static ArrayList topSort(ArrayList graph) { HashMap indegreeMap = new HashMap<>(); for (DirectedGraphNode cur : graph) { indegreeMap.put(cur, 0); } for (DirectedGraphNode cur : graph) { for (DirectedGraphNode next : cur.neighbors) { indegreeMap.put(next, indegreeMap.get(next) + 1); } } Queue zeroQueue = new LinkedList<>(); for (DirectedGraphNode cur : indegreeMap.keySet()) { if (indegreeMap.get(cur) == 0) { zeroQueue.add(cur); } } ArrayList ans = new ArrayList<>(); while (!zeroQueue.isEmpty()) { DirectedGraphNode cur = zeroQueue.poll(); ans.add(cur); for (DirectedGraphNode next : cur.neighbors) { indegreeMap.put(next, indegreeMap.get(next) - 1); if (indegreeMap.get(next) == 0) { zeroQueue.offer(next); } } } return ans; } }