0207.cpp (678B)
1 class Solution { 2 static int adj[2048][2048]; 3 4 public: 5 bool canFinish(int n, const vector<vector<int>> &prerequisites) { 6 int count[2048] = {0}, size[2048] = {0}; 7 8 for (auto &p : prerequisites) { 9 adj[p[0]][size[p[0]]++] = p[1]; 10 count[p[1]]++; 11 } 12 13 queue<int> q; 14 for (int i = 0; i < n; i++) 15 if (!count[i]) q.push(i); 16 17 while (!q.empty()) { 18 int root = q.front(); 19 q.pop(), n--; 20 for (int i = 0; i < size[root]; i++) 21 if (!--count[adj[root][i]]) q.push(adj[root][i]); 22 } 23 24 return n == 0; 25 } 26 }; 27 28 int Solution::adj[2048][2048];