LeetCode Ts-2872. Maximum Number of K-Divisible Components

    CodingLeetCode Js-DFS/BFS

    There is an undirected tree with n nodes labeled from 0 to n – 1. You are given the integer n and a 2D integer array edges of length n – 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
    You are also given a 0-indexed integer array values of length n, where values[i] is the value associated with the ith node, and an integer k.
    A valid split 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 k, where the value of a connected component is the sum of the values of its nodes.
    Return the maximum number of components in any valid split.

    這裡有一個包含 n 個節點的無相樹,節點的編號從 0 到 n - 1。
    已知整數 n 和一個長度 n - 1 的二維整數陣列 edges,其中 edges[i] = [ai, bi] 表示數中節點 ai 和 bi 之間存在一條邊。
    你也給定一個從 0 索引開始、長度為 n 的整數陣列 values,其中 values[i] 表示與第 i 個節點關聯的直,以及一個整數 k。
    樹的有效分割是透過從樹中移除任意一組邊(可能為空)得到的,使得分割後的所有連通的值都能被 k 整除,其中連通的值是其所有節點的總和。
    回傳任何有效分割中的最大數量。
    

    Example 1:

    Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
    Output: 2
    Explanation: We remove the edge connecting node 1 with 2. The resulting split is valid because:
    - The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12.
    - The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6.
    It can be shown that no other valid split has more than 2 connected components.
    

    Example 2:

    Input: 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
    Output: 3
    Explanation: We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because:
    - The value of the component containing node 0 is values[0] = 3.
    - The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9.
    - The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6.
    It can be shown that no other valid split has more than 3 connected components.
    

    Solution:
    我們先建立樹的鄰接表,接著 DFS 遍歷整棵樹,
    其中我們判斷子樹能不能被 k 整除,
    如果可以則表示這個子樹可以切成一個有效的元件,則 ans++。
    當 DFS 跑完之後的 ans 就會是可以切出最大 k 可整除的子樹數量。

    Code 1: BigO(n)

    function maxKDivisibleComponents(n: number, edges: number[][], values: number[], k: number): number {
        const graph: number[][] = Array.from({length: n}, () => []);
        for (const [u, v] of edges) {
            graph[u].push(v);
            graph[v].push(u);
        }
    
        const sum: number[] = new Array(n).fill(0);
        let ans: number = 0;
    
        function dfs(v: number, parent: number) {
            for (const u of graph[v]) {
                if (u !== parent) {
                    dfs(u, v);
                    sum[v] += sum[u];
                    sum[v] %= k;
                }
            }
            sum[v] += values[v];
            sum[v] %= k;
            if (sum[v] === 0) {
                ans++;
            }
        }
        dfs(0, -1);
        return ans;
    };
    

    FlowChart:
    Example 1

    Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
    
    Node 3 sum [ 0, 0, 0, 4, 0 ] ans 0
    Node 1 sum [ 0, 0, 0, 4, 0 ] ans 0
    Node 1 ans 1
    Node 4 sum [ 0, 0, 0, 4, 4 ] ans 1
    Node 2 sum [ 0, 0, 5, 4, 4 ] ans 1
    Node 0 sum [ 0, 0, 5, 4, 4 ] ans 1
    Node 0 ans 2
    

    Example 2

    Input: 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
    
    Node 3 sum [
      0, 0, 0, 1,
      0, 0, 0
    ] ans 0
    Node 4 sum [
      0, 1, 0, 1,
      2, 0, 0
    ] ans 0
    Node 1 sum [
      0, 0, 0, 1,
      2, 0, 0
    ] ans 0
    Node 1 ans 1
    Node 5 sum [
      0, 0, 0, 1,
      2, 2, 0
    ] ans 1
    Node 6 sum [
      0, 0, 2, 1,
      2, 2, 1
    ] ans 1
    Node 2 sum [
      0, 0, 0, 1,
      2, 2, 1
    ] ans 1
    Node 2 ans 2
    Node 0 sum [
      0, 0, 0, 1,
      2, 2, 1
    ] ans 2
    Node 0 ans 3