Skip to content

Commit 3110e2f

Browse files
committed
Runtime: 270 ms (Top 87.54%) | Memory: 69.3 MB (Top 32.97%)
1 parent ec08b55 commit 3110e2f

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
// Runtime: 270 ms (Top 87.54%) | Memory: 69.3 MB (Top 32.97%)
12
// question similar to find all ancestors of current node
23

3-
4-
54
class Solution {
6-
5+
76
public:
8-
7+
98
void dfs(vector<vector<int>>&adj,vector<set<int>>&Anc,int u,int par,vector<int>&vis)
109
{
1110
if( vis[u] != 0 )
1211
return ;
13-
12+
1413
vis[u] = 1 ;
15-
14+
1615
for(auto&v:adj[u])
1716
{
1817
if( vis[v] == 0 )
@@ -21,37 +20,36 @@ class Solution {
2120
dfs(adj,Anc,v,par,vis) ;
2221
}
2322
}
24-
23+
2524
return ;
2625
}
27-
26+
2827
vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {
29-
28+
3029
int n = numCourses ;
3130
vector<vector<int>>adj(n);
3231
vector<set<int>>Anc(n);
3332
vector<bool>ans;
34-
33+
3534
for(auto&edge:prerequisites)
3635
{
3736
adj[edge[0]].push_back(edge[1]) ;
3837
}
39-
38+
4039
for(int i=0;i<n;i++)
4140
{
4241
vector<int>vis(n,0);
4342
dfs(adj,Anc,i,i,vis) ;
4443
}
45-
44+
4645
for(auto&EachQuery:queries)
4746
{
48-
if( Anc[EachQuery[1]].find(EachQuery[0]) != Anc[EachQuery[1]].end() )
47+
if( Anc[EachQuery[1]].find(EachQuery[0]) != Anc[EachQuery[1]].end() )
4948
ans.push_back(true);
5049
else
5150
ans.push_back(false) ;
5251
}
5352
return ans ;
5453
}
55-
56-
};
5754

55+
};

0 commit comments

Comments
 (0)