-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2368.cpp
More file actions
24 lines (23 loc) · 781 Bytes
/
2368.cpp
File metadata and controls
24 lines (23 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int res = 0;
void dfs(int currNode, vector<vector<int>>& adjacency, vector<bool>& visited) {
visited[currNode] = true;
res++;
for (auto& neighbor : adjacency[currNode]) {
if (visited[neighbor]) continue;
dfs(neighbor, adjacency, visited);
}
}
int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {
vector<vector<int>> adjacency(n);
for (auto& edge : edges) {
adjacency[edge[0]].push_back(edge[1]);
adjacency[edge[1]].push_back(edge[0]);
}
vector<bool> visited(n, false);
for (auto& num : restricted) visited[num] = true;
dfs(0, adjacency, visited);
return res;
}
};