Problem · Tree
Neural Network Subnetwork Strength
Learn this problemProblem statement
A neural network has n neurons numbered from 1 to n.
If the ith neuron has strong connectivity, strongConnectivity[i] = 1. If it has weak connectivity, strongConnectivity[i] = 0.
The neurons form a tree-like network with n - 1 connections, where the ith connection connects neurons neuronFrom[i] and neuronTo[i].
A neuron's strength is defined as the maximum difference between strongly connected and weakly connected neurons in any subnetwork including that neuron.
Return an array of n integers, where the ith integer represents the strength of neuron i.
Note: A subnetwork is a connected subgraph of the given network.
Function
getNeuronStrengths(n: int, neuronFrom: int[], neuronTo: int[], strongConnectivity: int[]) → int[]Examples
Example 1
n = 4neuronFrom = [1, 1, 1]neuronTo = [2, 3, 4]strongConnectivity = [0, 0, 1, 0]return = [0, -1, 1, -1]Neuron 3 has strong connectivity.
- For neuron
1's strength, consider the subnetwork with neurons1and3:1strong,1weak, so strength is0. - For neuron
2's strength, consider the subnetwork with neurons1,2, and3:1strong,2weak, so strength is-1. - For neuron
3's strength, consider the subnetwork with only neuron3:1strong,0weak, so strength is1. - For neuron
4's strength, consider the subnetwork with only neuron4:0strong,1weak, so strength is-1.
The neuronStrengths array is [0, -1, 1, -1].