leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0684.cpp (771B)


      1 class Solution {
      2   public:
      3     int minReorder(int n, vector<vector<int>> &connections) {
      4         vector<vector<int>> adj(n, vector<int>());
      5         vector<bool> visited(n, false);
      6         stack<int> st;
      7         int res = 0;
      8 
      9         for (auto &e : connections) {
     10             adj[e[0]].push_back(e[1]);
     11             adj[e[1]].push_back(-e[0]);
     12         }
     13 
     14         st.push(0);
     15         visited[0] = true;
     16         while (!st.empty()) {
     17             int root = st.top();
     18             st.pop();
     19             for (auto c : adj[root]) {
     20                 int ac = abs(c);
     21                 if (!visited[ac]) {
     22                     res += c > 0;
     23                     visited[ac] = true;
     24                     st.push(ac);
     25                 }
     26             }
     27         }
     28         return res;
     29     }
     30 };