leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2368.cpp (854B)
0 class Solution {
1 public:
2 int reachableNodes(int n, vector<vector<int>> &edges, vector<int> &restricted) {
3 unordered_set<int> rest(restricted.begin(), restricted.end());
4 vector<vector<int>> adj(n, vector<int>());
6 for (auto &p : edges) {
7 if (rest.count(p[0]) || rest.count(p[1])) continue;
8 adj[p[0]].push_back(p[1]);
9 adj[p[1]].push_back(p[0]);
10 }
12 int res = 0;
13 stack<int> st;
14 vector<bool> visited(n, false);
15 st.push(0);
16 visited[0] = true;
17 while (!st.empty()) {
18 int root = st.top();
19 st.pop();
20 res++;
21 for (int c : adj[root])
22 if (!visited[c]) {
23 st.push(c);
24 visited[c] = true;
25 }
26 }
27 return res;
28 }
29 };