-
Notifications
You must be signed in to change notification settings - Fork 3
/
2440.cpp
47 lines (44 loc) · 1.48 KB
/
2440.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
public:
vector<vector<int>> adjacency;
vector<int> nums;
int dfs(int current, int parent, int& components, int targetVal) {
int sum = nums[current];
for (auto& neighbor : adjacency[current]) {
if (neighbor == parent) continue;
sum += dfs(neighbor, current, components, targetVal);
}
if (sum == targetVal) {
components--;
return 0;
}
return sum;
}
int isDivisible(int components, int targetVal) {
int tempComponents = components;
dfs(0, -1, components, targetVal);
if (components == 0) return tempComponents - 1;
return -1;
}
int componentValue(vector<int>& nums, vector<vector<int>>& edges) {
this->nums = nums;
int n = nums.size();
adjacency.resize(n);
for (auto& edge : edges) {
adjacency[edge[0]].push_back(edge[1]);
adjacency[edge[1]].push_back(edge[0]);
}
if (count(nums.begin(), nums.end(), nums[0]) == n) return n - 1;
int sum = accumulate(nums.begin(), nums.end(), 0);
int res = 0;
for (int i = 2; i * i <= sum; ++i) {
if (sum % i == 0) {
int components = i;
int targetVal = sum / i;
res = max(res, isDivisible(components, targetVal));
res = max(res, isDivisible(targetVal, components));
}
}
return res;
}
};