-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReachableNodesWithRestrictions.java
72 lines (59 loc) · 1.96 KB
/
ReachableNodesWithRestrictions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*https://leetcode.com/problems/reachable-nodes-with-restrictions/*/
class Solution {
public int reachableNodes(int n, int[][] edges, int[] restricted) {
int count = 0;
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
Queue<Integer> queue = new LinkedList<Integer>();
HashSet<Integer> restrictedSet = new HashSet<Integer>();
boolean[] visited = new boolean[n];
for (int i = 0; i < n; ++i)
graph.add(new ArrayList<Integer>());
for (int[] edge : edges)
{
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
}
for (int val : restricted)
restrictedSet.add(val);
queue.add(0);
while (!queue.isEmpty())
{
int node = queue.poll();
if (visited[node]) continue;
visited[node] = true;
if (restrictedSet.contains(node)) continue;
++count;
for (Integer adjNode : graph.get(node))
if (!visited[adjNode])
queue.add(adjNode);
}
return count;
}
}
class Solution {
List<Integer> [] graph;
boolean [] visited;
public int reachableNodes(int n, int[][] edges, int[] restricted) {
graph = new List[n];
visited = new boolean [n];
for (int i = 0; i < n; ++i)
graph[i] = new ArrayList<>();
for (int [] edge : edges){
graph[edge[0]].add(edge[1]);
graph[edge[1]].add(edge[0]);
}
for (int num : restricted)
visited[num] = true;
return dfs(0);
}
private int dfs(int node){
if (visited[node])
return 0;
visited[node] = true;
int result = 1;
for (int child : graph[node]){
result += dfs(child);
}
return result;
}
}