-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
create-components-with-same-value.cpp
135 lines (129 loc) · 4.13 KB
/
create-components-with-same-value.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Time: O(n * sqrt(n))
// Space: O(n)
// bfs, greedy
class Solution {
public:
int componentValue(vector<int>& nums, vector<vector<int>>& edges) {
vector<vector<int>> adj(size(nums));
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
const auto& bfs = [&](int target) {
vector<int> total(nums);
vector<int> cnt(size(nums));
for (int u = 0; u < size(nums); ++u) {
cnt[u] = size(adj[u]);
}
vector<int> q;
for (int u = 0; u < size(nums); ++u) {
if (cnt[u] == 1) {
q.emplace_back(u);
}
}
while (!empty(q)) {
vector<int> new_q;
for (const auto& u : q) {
if (total[u] > target) {
return false;
}
if (total[u] == target) {
total[u] = 0;
}
for (const auto& v : adj[u]) {
total[v] += total[u];
if (--cnt[v] == 1) {
new_q.emplace_back(v);
}
}
}
q = move(new_q);
}
return true;
};
const int total = accumulate(cbegin(nums), cend(nums), 0);
for (int cnt = size(nums); cnt >= 2; --cnt) {
if (total % cnt == 0 && bfs(total / cnt)) {
return cnt - 1;
}
}
return 0;
}
};
// Time: O(n * sqrt(n))
// Space: O(n)
// iterative dfs, greedy
class Solution2 {
public:
int componentValue(vector<int>& nums, vector<vector<int>>& edges) {
vector<vector<int>> adj(size(nums));
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
const auto& iter_dfs = [&](int target) {
vector<int> total(nums);
vector<tuple<int, int, int>> stk = {{1, 0, -1}};
while (!empty(stk)) {
const auto [step, u, p] = stk.back(); stk.pop_back();
if (step == 1) {
stk.emplace_back(2, u, p);
for (const auto& v : adj[u]) {
if (v == p) {
continue;
}
stk.emplace_back(1, v, u);
}
} else if (step == 2) {
for (const auto& v : adj[u]) {
if (v == p) {
continue;
}
total[u] += total[v];
}
if (total[u] == target) {
total[u] = 0;
}
}
}
return total[0];
};
const int total = accumulate(cbegin(nums), cend(nums), 0);
for (int cnt = size(nums); cnt >= 2; --cnt) {
if (total % cnt == 0 && iter_dfs(total / cnt) == 0) {
return cnt - 1;
}
}
return 0;
}
};
// Time: O(n * sqrt(n))
// Space: O(n)
// dfs, greedy
class Solution3 {
public:
int componentValue(vector<int>& nums, vector<vector<int>>& edges) {
vector<vector<int>> adj(size(nums));
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
const function<int(int, int, int)> dfs = [&](int u, int p, int target) {
int total = nums[u];
for (const auto& v : adj[u]) {
if (v == p) {
continue;
}
total += dfs(v, u, target);
}
return total != target ? total : 0;
};
const int total = accumulate(cbegin(nums), cend(nums), 0);
for (int cnt = size(nums); cnt >= 2; --cnt) {
if (total % cnt == 0 && dfs(0, -1, total / cnt) == 0) {
return cnt - 1;
}
}
return 0;
}
};