题目
解法1:BFS
class Solution:
def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
graph = collections.defaultdict(list)
# s:subordinates,m:direct manager
for s,m in enumerate(manager):
graph[m].append(s)
q = collections.deque()
q.append((headID,0))
ans = 0;
while q:
curr_m,time = q.popleft()
if curr_m not in graph:
ans = max(ans,time)
for curr_s in graph[curr_m]:
q.append((curr_s,informTime[curr_m]+time))
return ans
C++版本
这边表示图既可以用unordered_map也可以用vector和array结合。unordered_map会稍微慢一点,因为他的查找操作并不是严格O(1)的
class Solution {
public:
int numOfMinutes(int n, int headID, vector<int>& manager, vector<int