q = deque()
q.append(start)
visited = set()
visited.add(start)
while q:
u = q.popleft()
for v in adj[u]:
if v not in visited:
visited.add(v)
q.append(v)
Problems
2092. Find All People With Secret
- https://leetcode.com/problems/find-all-people-with-secret
- Revisit the node based if the secret is know to that node earliest. Revisit the node if better option is found in the later point.
- Multi source bfs revisiting the nodes if better option is found.
1q = deque()
2q.append([0, 0])
3q.append([0, firstPerson])
4earliest = [inf] * n
5earliest[0] = 0
6earliest[firstPerson] = 0
7while q:
8 time, u = q.popleft()
9 for t, v in adj[u]:
10 if t >= time and t < earliest[v]:
11 earliest[v] = t
12 q.append([t, v])