-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.cpp
39 lines (35 loc) · 1.04 KB
/
solution.cpp
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
class Solution
{
public:
vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>> &prerequisites, vector<vector<int>> &queries)
{
// Initialize the graph
vector<vector<bool>> graph(numCourses, vector<bool>(numCourses, false));
// Build the direct edges from prerequisites
for (const auto &edge : prerequisites)
{
graph[edge[0]][edge[1]] = true;
}
// Floyd-Warshall to compute transitive closure
for (int k = 0; k < numCourses; ++k)
{
for (int i = 0; i < numCourses; ++i)
{
for (int j = 0; j < numCourses; ++j)
{
if (graph[i][k] && graph[k][j])
{
graph[i][j] = true;
}
}
}
}
// Answer the queries
vector<bool> result;
for (const auto &query : queries)
{
result.push_back(graph[query[0]][query[1]]);
}
return result;
}
};