|
| 1 | +# [Problem 2872: Maximum Number of K-Divisible Components](https://leetcode.com/problems/maximum-number-of-k-divisible-components/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We have a tree and node values; we can remove some edges so that every resulting connected component has sum of node values divisible by k. The total sum is divisible by k (given), so at least the whole tree is valid (no cuts). Cutting an edge splits a subtree from the rest; the sum of that subtree must be divisible by k. That suggests we should look at subtree sums modulo k. |
| 5 | + |
| 6 | +If we root the tree somewhere (say node 0), for any non-root node u, if the sum of the subtree rooted at u is divisible by k, then we may cut the edge between u and its parent and get an extra valid component. Cutting such an edge doesn't break the property for other disjoint subtrees. Intuitively we can cut every edge whose child-subtree sum % k == 0 independently. So count how many non-root nodes have subtree sum % k == 0; answer is that count + 1 (components = cuts + 1). DFS to compute subtree sum modulo k seems natural. |
| 7 | + |
| 8 | +## Refining the problem, round 2 thoughts |
| 9 | +- Implementation detail: sums can be large but we only need them modulo k, so propagate s = (s + child_sum) % k. |
| 10 | +- When a subtree sum % k == 0 for node u (u != root), we can increment cut count and return 0 to parent (simulate cutting so parent doesn't include the subtree sum). |
| 11 | +- Edge cases: |
| 12 | + - n == 1 -> no edges, answer should be 1. |
| 13 | + - k == 1 -> every value % 1 == 0, every subtree sum % 1 == 0 -> we can cut all n-1 edges -> answer = n. |
| 14 | +- Complexity: one DFS over n nodes, O(n) time and O(n) space for adjacency + recursion stack. For Python recursion, increase recursion limit (n up to 3e4). |
| 15 | +- Correctness: cutting every edge where the child-subtree sum is 0 (mod k) is never harmful — those subtrees are independent of the rest and produce valid components. Since total sum divisible by k, root ends up with 0 mod k but we don't attempt to cut above root. |
| 16 | + |
| 17 | +## Attempted solution(s) |
| 18 | +```python |
| 19 | +import sys |
| 20 | +from typing import List |
| 21 | + |
| 22 | +class Solution: |
| 23 | + def maxKDivisibleComponents(self, n: int, edges: List[List[int]], values: List[int], k: int) -> int: |
| 24 | + # Build adjacency list |
| 25 | + adj = [[] for _ in range(n)] |
| 26 | + for u, v in edges: |
| 27 | + adj[u].append(v) |
| 28 | + adj[v].append(u) |
| 29 | + |
| 30 | + sys.setrecursionlimit(1000000) |
| 31 | + cuts = 0 |
| 32 | + |
| 33 | + def dfs(u: int, parent: int) -> int: |
| 34 | + nonlocal cuts |
| 35 | + # subtree sum modulo k |
| 36 | + s = values[u] % k |
| 37 | + for v in adj[u]: |
| 38 | + if v == parent: |
| 39 | + continue |
| 40 | + child_mod = dfs(v, u) |
| 41 | + s = (s + child_mod) % k |
| 42 | + # If this subtree (excluding parent) sums to 0 mod k and it's not the root, |
| 43 | + # we can cut the edge to parent and return 0 upwards. |
| 44 | + if parent != -1 and s == 0: |
| 45 | + cuts += 1 |
| 46 | + return 0 |
| 47 | + return s |
| 48 | + |
| 49 | + dfs(0, -1) |
| 50 | + # components = cuts + 1 (initial component + each cut increases count by 1) |
| 51 | + return cuts + 1 |
| 52 | + |
| 53 | +# Example usage: |
| 54 | +# sol = Solution() |
| 55 | +# print(sol.maxKDivisibleComponents(5, [[0,2],[1,2],[1,3],[2,4]], [1,8,1,4,4], 6)) # -> 2 |
| 56 | +``` |
| 57 | + |
| 58 | +- Notes about the solution: |
| 59 | + - Approach: Root the tree (node 0), DFS to compute subtree sums modulo k. Whenever a non-root subtree sums to 0 mod k, increment cut count and return 0 so parent doesn't include it. |
| 60 | + - Time complexity: O(n) — each node/edge visited once. |
| 61 | + - Space complexity: O(n) for adjacency list + O(n) recursion stack in worst case. |
| 62 | + - Implementation details: use modulo arithmetic to avoid large integers; increase recursion limit for deep trees. |
0 commit comments