-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtotal-sum-of-interaction-cost-in-tree-groups.cpp
More file actions
151 lines (145 loc) · 4.57 KB
/
total-sum-of-interaction-cost-in-tree-groups.cpp
File metadata and controls
151 lines (145 loc) · 4.57 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Time: O(n * g)
// Space: O(n * g)
// bfs
class Solution {
public:
long long interactionCosts(int n, vector<vector<int>>& edges, vector<int>& group) {
vector<vector<int>> adj(n);
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
const auto& mx = ranges::max(group);
vector<int64_t> total(mx);
for (const auto& x : group) {
++total[x - 1];
}
int64_t result = 0;
const auto& bfs = [&]() {
vector<int> order = {0};
vector<int> parent(n, -1);
for (int i = 0; i < size(adj); ++i) {
const auto u = order[i];
for (const auto& v : adj[u]) {
if (v == parent[u]) {
continue;
}
parent[v] = u;
order.emplace_back(v);
}
}
return pair(order, parent);
};
const auto& [order, parent] = bfs();
vector<vector<int64_t>> cnt(n, vector<int64_t>(mx));
for (int i = size(order) - 1; i >= 0; --i) {
const auto& u = order[i];
++cnt[u][group[u] - 1];
for (const auto& v : adj[u]) {
if (u != parent[v]) {
continue;
}
for (int k = 0; k < size(cnt[v]); ++k) {
result += cnt[v][k] * (total[k] - cnt[v][k]);
cnt[u][k] += cnt[v][k];
}
}
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
// bfs, small-to-large merging
class Solution2 {
public:
long long interactionCosts(int n, vector<vector<int>>& edges, vector<int>& group) {
vector<vector<int>> adj(n);
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
unordered_map<int, int64_t> total;
for (const auto& x : group) {
++total[x];
}
int64_t result = 0;
const auto& bfs = [&]() {
vector<int> order = {0};
vector<int> parent(n, -1);
for (int i = 0; i < size(adj); ++i) {
const auto u = order[i];
for (const auto& v : adj[u]) {
if (v == parent[u]) {
continue;
}
parent[v] = u;
order.emplace_back(v);
}
}
return pair(order, parent);
};
const auto& [order, parent] = bfs();
vector<unordered_map<int, int64_t>> cnt(n);
for (int i = size(order) - 1; i >= 0; --i) {
const auto& u = order[i];
++cnt[u][group[u]];
for (const auto& v : adj[u]) {
if (u != parent[v]) {
continue;
}
for (const auto& [k, c] : cnt[v]) {
result += c * (total[k] - c);
}
if (size(cnt[v]) > size(cnt[u])) {
swap(cnt[u], cnt[v]);
}
for (const auto& [k, c] : cnt[v]) {
cnt[u][k] += c;
}
cnt[v].clear();
}
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
// dfs, small-to-large merging
class Solution3 {
public:
long long interactionCosts(int n, vector<vector<int>>& edges, vector<int>& group) {
vector<vector<int>> adj(n);
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
unordered_map<int, int64_t> total;
for (const auto& x : group) {
++total[x];
}
int64_t result = 0;
const auto dfs = [&](this auto&& dfs, int u, int p) -> unordered_map<int, int64_t> {
unordered_map<int, int64_t> cnt;
++cnt[group[u]];
for (const auto& v : adj[u]) {
if (v == p) {
continue;
}
auto new_cnt = dfs(v, u);
for (const auto& [k, c] : new_cnt) {
result += c * (total[k] - c);
}
if (size(new_cnt) > size(cnt)) {
swap(cnt, new_cnt);
}
for (const auto& [k, c] : new_cnt) {
cnt[k] += c;
}
}
return cnt;
};
dfs(0, -1);
return result;
}
};