Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
511 B
2
Indexable
Never
def time_to_flower_tree(self, father: List[int], time: List[int]) -> int:
    # write your code here
    n = len(father)
    self.children = collections.defaultdict(list)
    for i in range(n):
        self.children[father[i]].append((i, time[i]))
    
    self.max_t = 0 
    self.dfs(0, 0)
    return self.max_t

def dfs(self, node, t):
    if node not in self.children:
        self.max_t = max(self.max_t, t)
        return 
    
    for child, cost in self.children[node]:
        self.dfs(child, t + cost)