Skip to content

2378. Choose Edges to Maximize Score in a Tree 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
 public:
  long long maxScore(vector<vector<int>>& edges) {
    const int n = edges.size();
    vector<vector<pair<int, int>>> graph(n);

    for (int i = 0; i < n; ++i) {
      const int parent = edges[i][0];
      const int weight = edges[i][1];
      if (parent != -1)
        graph[parent].emplace_back(i, weight);
    }

    const auto [takeRoot, notTakeRoot] = dfs(graph, 0);
    return max(takeRoot, notTakeRoot);
  }

 private:
  // Returns (the maximum sum at u if we take one u->v edge,
  //          the maximum sum at u if we don't take any child edge).
  pair<long, long> dfs(const vector<vector<pair<int, int>>>& graph, int u) {
    long bestEdge = 0;
    long notTakeU = 0;

    for (const auto& [v, w] : graph[u]) {
      const auto [takeV, notTakeV] = dfs(graph, v);
      bestEdge = max(bestEdge, w + notTakeV - takeV);
      notTakeU += takeV;
    }

    return {bestEdge + notTakeU, notTakeU};
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
  public long maxScore(int[][] edges) {
    final int n = edges.length;
    List<Pair<Integer, Integer>>[] graph = new List[n];

    for (int i = 0; i < n; ++i)
      graph[i] = new ArrayList<>();

    for (int i = 0; i < n; ++i) {
      final int parent = edges[i][0];
      final int weight = edges[i][1];
      if (parent != -1)
        graph[parent].add(new Pair<>(i, weight));
    }

    Pair<Long, Long> pair = dfs(graph, 0);
    final long takeRoot = pair.getKey();
    final long notTakeRoot = pair.getValue();
    return Math.max(takeRoot, notTakeRoot);
  }

  // Returns (the maximum sum at u if we take one u->v edge,
  //          the maximum sum at u if we don't take any child edge).
  private Pair<Long, Long> dfs(List<Pair<Integer, Integer>>[] graph, int u) {
    long bestEdge = 0;
    long notTakeU = 0;

    for (Pair<Integer, Integer> pair : graph[u]) {
      final int v = pair.getKey();
      final int w = pair.getValue();
      Pair<Long, Long> pair = dfs(graph, v);
      final long takeV = pair.getKey();
      final long notTakeV = pair.getValue();
      bestEdge = Math.max(bestEdge, w + notTakeV - takeV);
      notTakeU += takeV;
    }

    return new Pair<>(bestEdge + notTakeU, notTakeU);
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
  def maxScore(self, edges: list[list[int]]) -> int:
    n = len(edges)
    graph = [[] for _ in range(n)]

    for i, (parent, weight) in enumerate(edges):
      if parent != -1:
        graph[parent].append((i, weight))

    takeRoot, notTakeRoot = self._dfs(graph, 0)
    return max(takeRoot, notTakeRoot)

  def _dfs(self, graph: list[list[int]], u: int) -> tuple[int, int]:
    """
    Returns (the maximum sum at u if we take one u->v edge,
             the maximum sum at u if we don't take any child edge).
    """
    bestEdge = 0
    notTakeU = 0

    for v, w in graph[u]:
      takeV, notTakeV = self._dfs(graph, v)
      bestEdge = max(bestEdge, w + notTakeV - takeV)
      notTakeU += takeV

    return (bestEdge + notTakeU, notTakeU)