leetcode

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

2368.cpp (854B)


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