You are given a 2D integer array edges representing a tree with n nodes, numbered from 0 to n - 1, rooted at node 0, where edges[i] = [ui, vi]
means there is an edge between the nodes vi and ui.
You are also given a 0-indexed integer array colors of size n, where
colors[i] is the color assigned to node i.
We want to find a node v such that every node in the subtree of v has the
same color.
Return the size of such subtree with themaximum number of nodes possible.
Input: edges =[[0,1],[0,2],[0,3]], colors =[1,1,2,3]Output: 1Explanation: Each color is represented as:1-> Red,2-> Green,3-> Blue. We can see that the subtree rooted at node 0 has children with different colors. Any other subtree is of the same color and has a size of 1. Hence, we return1.
Example 2:
1
2
3
4
Input: edges =[[0,1],[0,2],[0,3]], colors =[1,1,1,1]Output: 4Explanation: The whole tree has the same color, and the subtree rooted at node 0 has the most number of nodes which is4. Hence, we return4.****
Example 3:
1
2
3
Input: edges =[[0,1],[0,2],[2,3],[2,4]], colors =[1,2,3,3,3]Output: 3Explanation: Each color is represented as:1-> Red,2-> Green,3-> Blue. We can see that the subtree rooted at node 0 has children with different colors. Any other subtree is of the same color, but the subtree rooted at node 2 has a size of 3 which is the maximum. Hence, we return3.
Constraints:
n == edges.length + 1
1 <= n <= 5 * 10^4
edges[i] == [ui, vi]
0 <= ui, vi < n
colors.length == n
1 <= colors[i] <= 10^5
The input is generated such that the graph represented by edges is a tree.
To find the largest subtree where all nodes have the same color, we can use DFS to traverse the tree. For each node, we check if all nodes in its subtree share the same color. If so, we update the maximum size found.
classSolution {
publicintmaximumSubtreeSize(int[][] edges, int[] colors) {
int n = colors.length, ans = 0;
List<List<Integer>> g =new ArrayList<>();
for (int i = 0; i < n; ++i) g.add(new ArrayList<>());
for (int[] e : edges) {
g.get(e[0]).add(e[1]);
g.get(e[1]).add(e[0]);
}
classHelper {
intdfs(int u, int p) {
int sz = 1;
boolean valid =true;
for (int v : g.get(u)) {
if (v == p) continue;
int subSz = dfs(v, u);
if (colors[v]!= colors[u]) valid =false;
sz += subSz;
}
if (valid) ans = Math.max(ans, sz);
return valid ? sz : 0;
}
}
new Helper().dfs(0, -1);
return ans;
}
}
classSolution {
funmaximumSubtreeSize(edges: Array<IntArray>, colors: IntArray): Int {
val n = colors.size
val g = Array(n) { mutableListOf<Int>() }
for (e in edges) {
g[e[0]].add(e[1])
g[e[1]].add(e[0])
}
var ans = 0fundfs(u: Int, p: Int): Pair<Boolean, Int> {
var sz = 1var valid = truefor (v in g[u]) {
if (v == p) continueval(ok, subSz) = dfs(v, u)
if (!ok || colors[v] != colors[u]) valid = false sz += subSz
}
if (valid) ans = maxOf(ans, sz)
return valid to sz
}
dfs(0, -1)
return ans
}
}
defmaximum_subtree_size(edges: list[list[int]], colors: list[int]) -> int:
from collections import defaultdict
n = len(colors)
g: dict[int, list[int]] = defaultdict(list)
for u, v in edges:
g[u].append(v)
g[v].append(u)
ans =0defdfs(u: int, p: int) -> tuple[bool, int]:
sz =1 valid =Truefor v in g[u]:
if v == p:
continue ok, sub_sz = dfs(v, u)
ifnot ok or colors[v] != colors[u]:
valid =False sz += sub_sz
if valid:
nonlocal ans
ans = max(ans, sz)
return valid, sz
dfs(0, -1)
return ans