2039. The Time When the Network Becomes Idle (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution:
    def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
        h = collections.defaultdict(set)
        for a, b in edges:
            h[a].add(b)
            h[b].add(a)
        seen = {0}
        result = 0
        q = [0]
        depth = 0
        while q:
            temp = []
            depth += 2
            for x in q:
                for num in h[x]:
                    if num not in seen:
                        temp.append(num)
                        seen.add(num)
                        res = depth
                        if depth >= patience[num]:
                            if depth % patience[num] != 0:
                                res += depth - depth % patience[num]
                            else:
                                res += depth - patience[num]
                        result = max(result, res)
            q = temp
        return result + 1