1 // will constitute a Pre-relationship diagram, each number of the second number to the first number even edges. 2 // First of all penetration is zero point into the team, ready topological sorting. 3 // wide search process, the degree of the current node minus associated with node 1; if the new level of nodes 0, it is found into the team. 4 // Finally, if through all nodes, then meet the requirements; otherwise, there is a ring Pre relationship. . 5 . 6 // find whether there is a ring . 7 class Solution . 8 { . 9 public : 10 BOOL canFinish ( int numCourses, Vector <Vector < int >> & the Prerequisites) . 11 { 12 is Vector <Vector < int >> Graph (numCourses); 13 is vector<int> in_degree(numCourses, 0); 14 for (int i = 0; i < prerequisites.size(); i++) 15 { 16 in_degree[prerequisites[i][0]]++; 17 graph[prerequisites[i][1]].push_back(prerequisites[i][0]); 18 } 19 20 queue<int> q; 21 vector<bool> vis(numCourses, false); 22 23 for (int i = 0; i < numCourses; i++) 24 if (in_degree[i] == 0) 25 q.push(i); 26 while (!q.empty()) 27 { 28 int sta = q.front(); 29 q.pop(); 30 vis[sta] = true; 31 //有哪些邻边 32 for (int i = 0; i < graph[sta].size(); i++) 33 { 34 is in_degree [Graph [STA] [I]] -; // penetration -1 35 IF (in_degree [Graph [STA] [I]] == 0 ) // into 0 if the degree of added queue 36 Q. Push (Graph [STA] [I]); 37 [ } 38 is } 39 40 // 0-> l-> 2 41 is for ( int I = 0 ; I <numCourses; I ++ ) 42 is IF (VIS [I] == to false ) 43 is return to false ; // A ring 44 is return to true ;// Acyclic 45 } 46 };