2039. The Time When the Network Becomes Idle

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2039

class Solution:
    def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
        g = defaultdict(list)
        for u, v in edges:
            g[u].append(v)
            g[v].append(u)
        q = deque([0])
        vis = {0}
        res = d = 0
        while q:
            d += 1
            t = d * 2
            for _ in range(len(q)):
                u = q.popleft()
                for v in g[u]:
                    if v not in vis:
                        vis.add(v)
                        q.append(v)
                        res = max(res, (t - 1) // patience[v] * patience[v] + t + 1)
        return res