// 有向图的环检测 public class DirectedCycleDetection { private Graph G; private boolean[] visited; private boolean[] onPath; //是否在当前搜索路径上 private boolean hasCycle = false; public DirectedCycleDetection(Graph G){ if(!G.isDirected()) throw new IllegalArgumentException("只支持有向图"); this.G = G; visited = new boolean[G.V()]; onPath = new boolean[G.V()]; // 多包一层for,防止有多个联通分量 for(int v = 0 ;v