Skip to content

Commit

Permalink
Time: 403 ms (66.29%), Space: 116.6 MB (45.20%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Mar 24, 2023
1 parent 55af0ba commit ed0bcd1
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int minReorder(int n, vector<vector<int>>& con) {
vector<vector<int>> adj(n) , bk(n);
vector<int> vis(n,0);
vis[0] =1;
for(auto a : con){
adj[a[0]].push_back(a[1]);
bk[a[1]].push_back(a[0]);
}
int ans=0;
queue<int> q;
q.push(0);
while(!q.empty()){
int a = q.front();
vis[a] =1;
q.pop();
for( int no : adj[a]){
if(!vis[no]){
ans++;
q.push(no);
}
}
for( int no : bk[a]){
if(!vis[no]){
q.push(no);
}
}
}
return ans;
}
};

0 comments on commit ed0bcd1

Please sign in to comment.