| 1 | class Solution { |
| 2 | public List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) { |
| 3 | boolean mat[][] = new boolean[numCourses][numCourses]; |
| 4 | for(int i=0;i<prerequisites.length;i++){ |
| 5 | int s = prerequisites[i][0]; |
| 6 | int d = prerequisites[i][1]; |
| 7 | mat[s][d] = true; |
| 8 | } |
| 9 | |
| 10 | for(int k=0;k<numCourses;k++){ |
| 11 | for(int s=0;s<numCourses;s++){ |
| 12 | for(int d=0;d<numCourses;d++){ |
| 13 | mat[s][d] = mat[s][d] || (mat[s][k] && mat[k][d]); |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | List<Boolean> ans = new ArrayList<>(); |
| 18 | for(int i=0;i<queries.length;i++){ |
| 19 | int s = queries[i][0]; |
| 20 | int d = queries[i][1]; |
| 21 | ans.add(mat[s][d]); |
| 22 | } |
| 23 | return ans; |
| 24 | } |
| 25 | } |