Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int minReorder(int n, vector<vector<int>>& connections) {
- vector<int> graph[n];
- for(auto edge: connections){
- graph[edge[0]].push_back(edge[1]);
- graph[edge[1]].push_back(-edge[0]);
- }
- bool vis[n];
- memset(vis, 0, sizeof(vis));
- int ans = 0;
- queue<int> q;
- q.push(0);
- vis[0] = true;
- while(!q.empty()){
- int curr = q.front();
- q.pop();
- for(int next: graph[curr]){
- if(!vis[abs(next)]){
- vis[abs(next)] = true;
- q.push(abs(next));
- ans += (next>0);
- }
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement