There is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
At every node i, there is a gate. You are also given an array of even integers amount, where amount[i] represents:
the price needed to open the gate at node i, if amount[i] is negative, or,
the cash reward obtained on opening the gate at node i, otherwise.
The game goes on as follows:
Initially, Alice is at node 0 and Bob is at node bob.
At every second, Alice and Bob each move to an adjacent node. Alice moves towards some leaf node, while Bob moves towards node 0.
For every node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
If the gate is already open, no price will be required, nor will there be any cash reward.
If Alice and Bob reach the node simultaneously, they share the price/reward for opening the gate there. In other words, if the price to open the gate is c, then both Alice and Bob pay c / 2 each. Similarly, if the reward at the gate is c, both of them receive c / 2 each.
If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node 0, he stops moving. Note that these events are independent of each other.
Return the maximum net income Alice can have if she travels towards the optimal leaf node.
0부터 n-1까지 레이블이 지정된 n개의 노드로 이루어진 무방향 트리가 있으며, 루트는 노드 0입니다. n-1 길이의 2D 정수 배열 edges
가 주어지며, 여기서 edges[i] = [ai, bi]
는 트리에서 노드 ai
와 노드 bi
사이에 간선이 있음을 나타냅니다.
각 노드 i
에는 게이트가 있습니다. 또한 짝수 정수 배열 amount
가 주어지며, amount[i]
는 다음을 나타냅니다:
amount[i]
가 음수이면 노드 i
의 게이트를 여는 데 필요한 비용,i
에서 게이트를 열 때 얻는 현금 보상.게임은 다음과 같이 진행됩니다:
bob
에 있습니다.c
라면 둘 다 c / 2
씩 지불하고, 보상이 c
라면 둘 다 c / 2
씩 받습니다.앨리스가 최적의 리프 노드를 향해 이동할 때 얻을 수 있는 최대 순수익을 반환하세요.
입력: edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
출력: 6
설명:
위의 다이어그램은 주어진 트리를 나타냅니다. 게임은 다음과 같이 진행됩니다:
-2
가 됩니다. -2 + (4 / 2) = 0
이 됩니다. 0 + 6 = 6
이 됩니다.class Solution:
def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int:
graph = defaultdict(list)
degree = [0] * len(amount)
for e1, e2 in edges:
graph[e1].append(e2)
graph[e2].append(e1)
degree[e1] += 1
degree[e2] += 1
leaves = set()
for i in range(len(amount)):
if degree[i] == 1 and i != 0:
leaves.add(i)
path = []
chk = [False] * len(amount)
def pathfinder(current: int, path_list: list) -> bool:
nonlocal path
path_list.append(current)
chk[current] = True
if current == 0:
path = path_list[:]
return True
for to in graph[current]:
if not chk[to]:
if pathfinder(to, path_list):
return True
path_list.pop()
return False
pathfinder(bob, [])
bobtime = [-1] * len(amount)
i = 0
for p in path:
bobtime[p] = i
i += 1
bfs = deque()
chk = [False] * len(amount)
bfs.append((0, 0, amount[0]))
ans = -float('inf')
while bfs:
position, time, value = bfs.popleft()
chk[position] = True
if position in leaves:
ans = max(ans, value)
continue
for to in graph[position]:
if chk[to]:
continue
if bobtime[to] == -1 or bobtime[to] > time + 1:
bfs.append((to, time + 1, value + amount[to]))
elif bobtime[to] == time + 1:
bfs.append((to, time + 1, value + (amount[to] // 2)))
else:
bfs.append((to, time + 1, value))
return ans