//方法一 入度表(广度优先遍历) public boolean canFinish(int numCourses,int[][] prerequisites){ int[] indegrees = new int[numCourses]; List<List<Integer>> adjacency = new ArrayList<List<Integer>>(); Queue<Integer> queue = new LinkedList<>(); for(int i=0;i<numCourses;i++){ adjacency.add(new ArrayList<Integer>()); } for(int[] cp:prerequisites){ indegrees[cp[0]]++; adjacency.get(cp[1]).add(cp[0]); } for(int i=0;i<numCourses;i++){ if(indegrees[i]==0){ queue.add(i); } } while(!queue.isEmpty()){ int pre = queue.poll(); numCourses--; for(int cur:adjacency.get(pre)){ if(--indegrees[cur]==0){ queue.add(cur); } } } return numCourses == 0; }
// 方法二 深度优先遍历 public boolean canFinish2(int numCourses,int[][] prerequisites){ List<List<Integer>> adjacency = new ArrayList<List<Integer>>(); for(int i=0;i<numCourses;i++){ adjacency.add(new ArrayList<Integer>()); } for(int[] cp:prerequisites){ adjacency.get(cp[1]).add(cp[0]); } int[] flags = new int[numCourses]; for(int i=0;i<numCourses;i++){ if(!dfs(adjacency,flags,i)){ return false; } } return true; } private boolean dfs(List<List<Integer>> adjacency,int[] flags,int i){ if(flags[i] == 1){ return false; } if(flags[i]== -1){ return true; } flags[i] = 1; for(Integer j:adjacency.get(i)){ if(!dfs(adjacency, flags, j)){ return false; } } flags[i] = -1; return true; }