diff --git a/2872. Maximum Number of K-Divisible Components b/2872. Maximum Number of K-Divisible Components new file mode 100644 index 0000000..4a56705 --- /dev/null +++ b/2872. Maximum Number of K-Divisible Components @@ -0,0 +1,28 @@ +class Solution { +public: + int ans = 0; + + int dfs(int num, int& k, vector &values, vector> &vt, int parent) { + + int total = values[num]; + + for (const int &x : vt[num]) + total += x == parent ? 0 : dfs(x, k, values, vt, num); + + total %= k; + ans += !total; + + return total; + }; + + int maxKDivisibleComponents(int n, vector>& edges, vector& values, int k) { + vector> vt(n); + for (const vector& edge : edges) { + int f = edge.front(), b = edge.back(); + vt[f].push_back(b); + vt[b].push_back(f); + } + dfs(0, k, values, vt, 0); + return ans; + } +};