Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
There is an undirected tree with n
nodes labeled from 0
to n - 1
and n - 1
edges.
You are given a 2D integer array edges
of length n - 1
where edges[i] = [ai, bi]
indicates that there is an edge between nodes ai
and bi
in the tree. You are also given an integer array restricted
which represents restricted nodes.
Return the maximum number of nodes you can reach from node 0
without visiting a restricted node.
Note that node 0
will not be a restricted node.
Example 1:
Input: n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
Output: 4
Explanation: The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
Example 2:
Input: n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
Output: 3
Explanation: The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
Constraints:
2 <= n <= 105
edges.length == n - 1
edges[i].length == 2
0 <= ai, bi < n
ai != bi
edges
represents a valid tree.1 <= restricted.length < n
1 <= restricted[i] < n
restricted
are unique.class Solution {
public int reachableNodes(int n, int[][] edges, int[] restricted) {
List<Integer>[] al = new ArrayList[n];
for(int i=0;i<n;i++) al[i] = new ArrayList<>();
for(int e[] : edges){
al[e[0]].add(e[1]); al[e[1]].add(e[0]);
}
Set<Integer> set = new HashSet<>();
for(int i : restricted) set.add(i);
Queue<Integer> q = new LinkedList<>();
q.add(0); set.add(0);
int ans = 0;
while(!q.isEmpty()){
int size = q.size();
while(size-->0){
ans++;
int curr = q.remove();
for(int next : al[curr])
if(set.add(next)) q.add(next);
}
}
return ans;
}
}
int reachableNodes(int n, vector<vector<int>>&e , vector<int>& r) {
unordered_set<int> s(begin(r),end(r));
vector<vector<int>> graph(n);
for(auto i:e) graph[i[0]].push_front(i[1]) , graph[i[1]].push_front(i[0]);
queue<int> q;
vector<bool> seen(n);
int cnt=0;
q.push(0);
seen[0]=true;
while(q.size()){
auto node= q.front(); q.pop();
if(s.count(node)) continue;
cnt++;
for(auto j:graph[node]) //Put Neighbours
if(!seen[j]) seen[j]=true , q.push(j);
}
return cnt;
}
class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
res = set(restricted)
graph = defaultdict(list)
vis = set()
for u, v in edges:
if u in res or v in res: continue
graph[u].append(v)
graph[v].append(u)
def dfs(node):
r = 1
for nei in graph[node]:
if nei in vis: continue
vis.add(nei)
r += dfs(nei)
return r
vis.add(0)
return dfs(0)
In our experience, we suggest you solve this Reachable Nodes With Restrictions LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Reachable Nodes With Restrictions LeetCode Solution
I hope this Reachable Nodes With Restrictions LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>