forked from Nanduag0/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCourseScheduleIV.txt
40 lines (36 loc) · 1.67 KB
/
CourseScheduleIV.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>>& prerequisites, vector<vector<int>>& queries)
{
vector<int> adj[numCourses];
vector<vector<int>> isReac(numCourses,vector<int>(numCourses,INT_MAX/2));
for(int i=0;i<prerequisites.size();i++)
{
isReac[prerequisites[i][0]][prerequisites[i][1]]=1;
}
for(int k=0;k<numCourses;k++)
{
for(int i=0;i<numCourses;i++)
{
for(int j=0;j<numCourses;j++)
{
if(isReac[i][j]>isReac[i][k]+isReac[k][j])
{
isReac[i][j]=isReac[i][k]+isReac[k][j];
}
}
}
}
vector<bool> ans;
for(int i=0;i<queries.size();i++)
{
if(isReac[queries[i][0]][queries[i][1]]!=INT_MAX/2)
{
ans.push_back(true);
}
else
ans.push_back(false);
}
return ans;
}
};