← 返回 snapchat 的题目列表Tree Game
类型:online_judge
In a tree structure, Player 1 starts moving downwards from the root node, while Player 2 can start from any node to move upwards. Each node has a certain amount of gold dust, and each player's goal is to collect as much gold dust as possible. Design an algorithm to calculate the maximum amount of gold dust each player can collect. Nodes' weights are given as integers, and the tree is provided as an adjacency list.
Input
The number of nodes n, where 1 <= n <= 1000.
Adjacency list edgeList, where edgeList[i] = [u, v, weight] indicates an edge between node u and v with weight.
Output
Maximum amount of gold dust each player can collect.
Example Input/Output:
Input: n = 3, edgeList = [[1, 2, 2], [2, 3, 3]] Output: [5, 5]
Input: n = 4, edgeList = [[1, 2, 1], [1, 3, 4], [3, 4, 5]] Output: [10, 9]
Example
Input
3
[[1, 2, 2], [2, 3, 3]]