Skip to content

3004. Maximum Subtree of the Same Color 👍

  • 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
34
35
36
37
class Solution {
 public:
  int maximumSubtreeSize(vector<vector<int>>& edges, vector<int>& colors) {
    int ans = 1;
    vector<vector<int>> tree(colors.size());

    for (const vector<int>& edge : edges) {
      const int u = edge[0];
      const int v = edge[1];
      tree[u].push_back(v);
    }

    dfs(tree, 0, colors, ans);
    return ans;
  }

 private:
  // Returns the size of subtree of u if every node in the subtree has the same
  // color. Otherwise, returns -1.
  int dfs(const vector<vector<int>>& tree, int u, const vector<int>& colors,
          int& ans) {
    int res = 1;
    for (const int v : tree[u]) {
      if (colors[v] != colors[u])
        res = -1;
      // If any node in the subtree of v has a different color, the result of
      // the subtree of u will be -1 as well.
      const int subtreeSize = dfs(tree, v, colors, ans);
      if (subtreeSize == -1)
        res = -1;
      else if (res != -1)
        res += subtreeSize;
    }
    ans = max(ans, res);
    return res;
  }
};
 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
class Solution {
  public int maximumSubtreeSize(int[][] edges, int[] colors) {
    List<Integer>[] tree = new ArrayList[colors.length];

    for (int i = 0; i < colors.length; ++i)
      tree[i] = new ArrayList<>();

    for (int[] edge : edges) {
      final int u = edge[0];
      final int v = edge[1];
      tree[u].add(v);
    }

    dfs(tree, 0, colors);
    return ans;
  }

  private int ans = 1;

  // Returns the size of subtree of u if every node in the subtree has the same
  // color. Otherwise, returns -1.
  private int dfs(List<Integer>[] tree, int u, int[] colors) {
    int res = 1;
    for (int v : tree[u]) {
      if (colors[v] != colors[u])
        res = -1;
      // If any node in the subtree of v has a different color, the result of
      // the subtree of u will be -1 as well.
      int subtreeSize = dfs(tree, v, colors);
      if (subtreeSize == -1)
        res = -1;
      else if (res != -1)
        res += subtreeSize;
    }
    ans = Math.max(ans, res);
    return res;
  }
}
 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
class Solution:
  def maximumSubtreeSize(
      self,
      edges: list[list[int]],
      colors: list[int],
  ) -> int:
    ans = 1
    tree = [[] for _ in range(len(colors))]

    for u, v in edges:
      tree[u].append(v)

    def dfs(u: int) -> int:
      """
      Returns the size of subtree of u if every node in the subtree has the same
      color. Otherwise, returns -1.
      """
      nonlocal ans
      res = 1
      for v in tree[u]:
        if colors[v] != colors[u]:
          res = -1
        # If any node in the subtree of v has a different color, the result of
        # the subtree of u will be -1 as well.
        subtreeSize = dfs(v)
        if subtreeSize == -1:
          res = -1
        elif res != -1:
          res += subtreeSize
      ans = max(ans, res)
      return res

    dfs(0)
    return ans