C++ || BFS || 95% FASTER || Commented Code with explanation
\nclass Solution {\nprivate:\n    bool can_divide_evenly(int comp_val, queue<int> leaf_q, vector<int> cnt_nghbr, vector<int> nums, vector<vector<int>> &G) {\n  
```\nclass Solution {\nprivate:\n    bool can_divide_evenly(int comp_val, queue<int> leaf_q, vector<int> cnt_nghbr, vector<int> nums, vector<vector<int>> &G) {\n        // we will apply topological sort in some form,\n        // roughly similar to BFS, by taking leaf nodes\n        // and merging them to their parents.\n        while(!leaf_q.empty()) {\n            // only leaf nodes, i.e nodes with cnt_nghbr == 1\n            // or degree == 1 will enter the queue\n            int idx = leaf_q.front();\n            leaf_q.pop();\n            \n            // since we are processing the leaf node, by merging\n            // it into its nghbr decrease its cnt_nghbr to 0\n            cnt_nghbr[idx]--;\n            \n            // 3 CASES\n                \n            // CASE 1: if value at idx is greater than the allowed\n            // comp_val because of some reason, we can\'t divide\n            // evenly into components\n            if(nums[idx] > comp_val)\n                return false;\n            \n            for(int &nghbr: G[idx]) {\n                \n                // optimisation: if we\'ve already processed a leaf node\n                // i.e. if a node\'s degree is zero we will not update its \n                // value, becuase it doesn\'t make sense as it will never\n                // enter the queue again\n                if(cnt_nghbr[nghbr] == 0)\n                    continue;\n                \n                // CASE 2: if value at idx is equal to comp_val\n                // just decrease the neighbout count, becuase\n                // we just completed a component with desired value\n                // and can act like we remove that edge\n                else if(nums[idx] == comp_val)\n                    cnt_nghbr[nghbr]--;\n                \n                // CASE 3: if value is less than the comp_val\n                // we add the value to the parent/nghbr node\n                // and decrease its neighbour\'s degree by 1\n                else {\n                    nums[nghbr] += nums[idx];\n                    cnt_nghbr[nghbr]--;\n                }\n                \n                // if nghbr under consideration becomes leaf node\n                // push it to the leaf queue\n                if(cnt_nghbr[nghbr] == 1)\n                    leaf_q.push(nghbr);\n            }\n        }\n        return true;\n    }\n    \npublic:\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        int n = nums.size();\n        \n        // checking if graph contains all the same value\n        int sum = accumulate(nums.begin(), nums.end(), 0);\n        int max_ele = *max_element(nums.begin(), nums.end());\n        if(sum % n == 0 && count(nums.begin(), nums.end(), sum/n) == n)\n            return n-1;\n        \n        // make adjacency list for the tree(graph)\n        vector<vector<int>> G(n);\n        for(vector<int> &e: edges) {\n            G[e[0]].push_back(e[1]);\n            G[e[1]].push_back(e[0]);\n        }\n        \n        // will store the indices of leaf nodes\n        queue<int> leaf_q;\n        // will sotre the count of neighbors of each node\n        vector<int> cnt_nghbr(n, 0);\n        for(int i = 0; i < n; ++i) {\n            if(G[i].size() == 1) leaf_q.push(i);\n            cnt_nghbr[i] += G[i].size();\n        }\n        \n        // optimisations: \n        // 1: start with minimum comp_val possible so that we \n        //    can get the maximum possible number of components\n        //    and hence can early stop our solution.\n        // 2: comp_val can\'t be less than the max element\n        // 3: comp_val cna\'t be more than sum\n        //    i.e. only one component, no edges removed\n        for(int comp_val = max_ele; comp_val <= sum; ++comp_val) {\n            // if sum cannot be divided evenly into num_comp\n            // with comp_val calues, then continue\n            if(sum % comp_val) continue;\n            int num_comp = sum / comp_val;\n            // because to make "num_comp" componenets\n            // we need to remove "num_comp-1" edges \n            if(can_divide_evenly(comp_val, leaf_q, cnt_nghbr, nums, G)) return num_comp - 1;\n        }\n        \n        // will never be called b/c in loop condition \n        // is comp_val == sum, so it will return 0 anyways\n        // there itself if needed\n        return 0;\n    }\n};```
[]