-
Notifications
You must be signed in to change notification settings - Fork 1
/
CourseSchedule4.cpp
68 lines (68 loc) · 2 KB
/
CourseSchedule4.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution {
public:
vector<bool> checkIfPrerequisite(int n, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {
int i,j,k,sz = prerequisites.size();
vector<vector<int>>dp(n,vector<int>(n,false));
//for(i=0;i<n;i++)
// dp[i][i]=true;
for(i=0;i<sz;i++)
dp[prerequisites[i][0]][prerequisites[i][1]]=true;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
dp[i][j]=(dp[i][j]||(dp[i][k] && dp[k][j]));
}
}
sz = queries.size();
vector<bool>res;
for(i=0;i<sz;i++)
{
if(dp[queries[i][0]][queries[i][1]])
res.push_back(true);
else
res.push_back(false);
}
return res;
/*int i,j,sz = prerequisites.size(),fron,neigh;
unordered_map<int, unordered_set<int>>graph;
unordered_map<int, unordered_set<int>>preq;
vector<int>indegree(n,0);
vector<bool>res;
queue<int>q;
for(i=0;i<sz;i++)
{
graph[prerequisites[i][0]].insert(prerequisites[i][1]);
indegree[prerequisites[i][1]]++;
}
for(i=0;i<n;i++)
{
if(indegree[i]==0)
q.push(i);
}
while(!q.empty())
{
fron = q.front();
q.pop();
for(auto it = graph[fron].begin();it!=graph[fron].end();it++)
{
neigh = *it;
preq[neigh].insert(fron);
preq[neigh].insert(preq[fron].begin(),preq[fron].end());
indegree[neigh]--;
if(indegree[neigh]==0)
q.push(neigh);
}
}
sz = queries.size();
for(i=0;i<sz;i++)
{
if(preq[queries[i][1]].count(queries[i][0]))
res.push_back(true);
else
res.push_back(false);
}
return res;*/
}
};