Back to problems

Split Drainage Tree

Algorithm · Two Sigma · Medium

You are given a sewer drainage system modeled as a rooted tree with n nodes labeled 0 to n-1. Water flows upwards from each node toward the root 0, where it exits the system. The tree structure is provided by two arrays, both of length n: parent[i]: the immediate parent of node i in the drainage path; parent[0] is -1 since the root has no parent. input[i]: the volume of water that directly enters the system at node i (this does not include water arriving from its children).…

Checking your access…