|
| 1 | +<h2><a href="https://leetcode.com/problems/maximum-number-of-k-divisible-components">3058. Maximum Number of K-Divisible Components</a></h2><h3>Hard</h3><hr><p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given the integer <code>n</code> and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p> |
| 2 | + |
| 3 | +<p>You are also given a <strong>0-indexed</strong> integer array <code>values</code> of length <code>n</code>, where <code>values[i]</code> is the <strong>value</strong> associated with the <code>i<sup>th</sup></code> node, and an integer <code>k</code>.</p> |
| 4 | + |
| 5 | +<p>A <strong>valid split</strong> of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by <code>k</code>, where the <strong>value of a connected component</strong> is the sum of the values of its nodes.</p> |
| 6 | + |
| 7 | +<p>Return <em>the <strong>maximum number of components</strong> in any valid split</em>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | +<img alt="" src="https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg" style="width: 1024px; height: 453px;" /> |
| 12 | +<pre> |
| 13 | +<strong>Input:</strong> n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6 |
| 14 | +<strong>Output:</strong> 2 |
| 15 | +<strong>Explanation:</strong> We remove the edge connecting node 1 with 2. The resulting split is valid because: |
| 16 | +- The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12. |
| 17 | +- The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6. |
| 18 | +It can be shown that no other valid split has more than 2 connected components.</pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | +<img alt="" src="https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg" style="width: 999px; height: 338px;" /> |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3 |
| 24 | +<strong>Output:</strong> 3 |
| 25 | +<strong>Explanation:</strong> We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because: |
| 26 | +- The value of the component containing node 0 is values[0] = 3. |
| 27 | +- The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9. |
| 28 | +- The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6. |
| 29 | +It can be shown that no other valid split has more than 3 connected components. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>1 <= n <= 3 * 10<sup>4</sup></code></li> |
| 37 | + <li><code>edges.length == n - 1</code></li> |
| 38 | + <li><code>edges[i].length == 2</code></li> |
| 39 | + <li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li> |
| 40 | + <li><code>values.length == n</code></li> |
| 41 | + <li><code>0 <= values[i] <= 10<sup>9</sup></code></li> |
| 42 | + <li><code>1 <= k <= 10<sup>9</sup></code></li> |
| 43 | + <li>Sum of <code>values</code> is divisible by <code>k</code>.</li> |
| 44 | + <li>The input is generated such that <code>edges</code> represents a valid tree.</li> |
| 45 | +</ul> |
0 commit comments