As reported: Microsoft online assessment (Sep 2025).
You are given a tree with n nodes (0 … n−1) and n − 1 edges. Each node holds a value 0 or 1. The value of a connected subgraph (a subtree) is
(number of nodes holding 1) − (number of nodes holding 0)
For every node, return the maximum value of a connected subgraph that contains that node.
Example 1
n = 4, values = [0,0,1,0], edges = [[0,1],[0,2],[0,3]] -> [0,-1,1,-1]
- Node 0: take
{0, 2}→1 − 1 = 0. - Node 1: take
{1, 0, 2}→1 − 2 = −1. - Node 2: take
{2}alone →1. - Node 3: take
{3, 0, 2}→−1.
Example 2
n = 6, values = [1,0,0,1,1,0], edges = [[0,1],[1,2],[2,3],[3,4],[1,5]] -> [1,1,1,2,2,0]
Constraints: 1 ≤ n ≤ 10^5; values[i] is 0 or 1; edges form a tree.
Code runner format: one JSON value per line: n, values, edges. Implement maxSubgraphValues(n, values, edges); the harness prints the list of n answers.