-
Notifications
You must be signed in to change notification settings - Fork 3
/
2603.cpp
58 lines (54 loc) · 1.79 KB
/
2603.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
48
49
50
51
52
53
54
55
56
57
58
class Solution {
public:
void removeEdge(vector<int>& v, int p) {
for (int i = 0; i < v.size(); ++i) {
if (v[i] == p) {
v.erase(v.begin() + i);
return;
}
}
}
int collectTheCoins(vector<int>& coins, vector<vector<int>>& edges) {
int sum = accumulate(coins.begin(), coins.end(), 0);
if (sum <= 1) return 0;
int n = coins.size();
int ans = edges.size();
vector<vector<int>> adjacency(n);
for (auto& edge : edges) {
adjacency[edge[0]].push_back(edge[1]);
adjacency[edge[1]].push_back(edge[0]);
}
vector<int> leaves;
for (int i = 0; i < n; ++i) {
if (adjacency[i].size() == 1) leaves.push_back(i);
}
vector<int> leavesWithCoin;
for (auto leaf : leaves) {
if (coins[leaf] > 0) {
leavesWithCoin.push_back(leaf);
continue;
}
while (coins[leaf] == 0 && adjacency[leaf].size() == 1) {
int parent = adjacency[leaf][0];
removeEdge(adjacency[leaf], parent);
removeEdge(adjacency[parent], leaf);
ans -= 1;
leaf = parent;
}
if (adjacency[leaf].size() == 1) {
leavesWithCoin.push_back(leaf);
}
}
int finalLeaves = 0;
for (auto leaf : leavesWithCoin) {
int parent = adjacency[leaf][0];
removeEdge(adjacency[leaf], parent);
removeEdge(adjacency[parent], leaf);
ans -= 1;
if (adjacency[parent].size() == 1) finalLeaves++;
}
ans -= finalLeaves;
ans *= 2;
return max(0, ans);
}
};