-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2872.cpp
More file actions
49 lines (40 loc) · 1.19 KB
/
Copy path2872.cpp
File metadata and controls
49 lines (40 loc) · 1.19 KB
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
48
49
class Solution {
public:
int maxKDivisibleComponents(int n, const vector<vector<int>> &edges, const vector<int> &values,
int k) const {
if (n < 2) return 1;
vector<long long> lvalues(begin(values), end(values));
vector<vector<int>> adj(n);
vector<int> count(n);
for (const auto &e : edges) {
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
count[e[0]]++;
count[e[1]]++;
}
queue<int> q;
for (int i = 0; i < n; i++) {
if (count[i] != 1) continue;
q.push(i);
}
int res = 0;
while (!q.empty()) {
const auto crnt = q.front();
q.pop();
count[crnt]--;
long long add = 0;
if (lvalues[crnt] % k == 0)
res++;
else
add = lvalues[crnt];
for (const auto next : adj[crnt]) {
if (count[next] == 0) continue;
lvalues[next] += add;
if (--count[next] == 1) {
q.push(next);
}
}
}
return res;
}
};