As reported: Microsoft online assessment (Nov 2025), question 2: the neural-network strength problem below.
A neural network has n neurons numbered 1 … n. strongConnectivity[i] = 1 if neuron i + 1 has strong connectivity and 0 if it is weak. The neurons form a tree with n − 1 connections; the i-th connection joins neuronFrom[i] and neuronTo[i].
The strength of a neuron is the maximum, over all subnetworks (connected subgraphs of the network) that include it, of (number of strongly connected neurons) − (number of weakly connected neurons) in the subnetwork.
Return an array of n integers whose i-th value is the strength of neuron i + 1.
Example 1
n = 4, strongConnectivity = [1,0,1,1], neuronFrom = [1,1,2], neuronTo = [2,3,4]
-> [2,2,2,2]
Taking the whole network gives 3 − 1 = 2 for everyone ({1, 3} also gives 2 for neurons 1 and 3).
Example 2
n = 5, strongConnectivity = [0,0,1,0,1], neuronFrom = [1,2,3,4], neuronTo = [2,3,4,5]
-> [-1,0,1,1,1]
A chain 1–2–3–4–5 with values −1, −1, +1, −1, +1. Neuron 1 can do no better than −1; neuron 2's best is {2, 3} = 0.
Constraints: 1 ≤ n ≤ 10^5; the connections form a tree.
Code runner format: one JSON value per line: n, strongConnectivity, neuronFrom, neuronTo. Implement neuronStrength(n, strongConnectivity, neuronFrom, neuronTo); the harness prints the array.