Advertisement
nikunjsoni

1466

Apr 25th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minReorder(int n, vector<vector<int>>& connections) {
  4.         vector<int> graph[n];
  5.         for(auto edge: connections){
  6.             graph[edge[0]].push_back(edge[1]);
  7.             graph[edge[1]].push_back(-edge[0]);
  8.         }
  9.         bool vis[n];
  10.         memset(vis, 0, sizeof(vis));
  11.         int ans = 0;
  12.         queue<int> q;
  13.         q.push(0);
  14.         vis[0] = true;
  15.         while(!q.empty()){
  16.             int curr = q.front();
  17.             q.pop();
  18.             for(int next: graph[curr]){
  19.                 if(!vis[abs(next)]){
  20.                     vis[abs(next)] = true;
  21.                     q.push(abs(next));
  22.                     ans += (next>0);
  23.                 }
  24.             }
  25.         }
  26.         return ans;
  27.     }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement