-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0834.SumOfDistancesInTree.cpp
More file actions
56 lines (47 loc) · 1.27 KB
/
0834.SumOfDistancesInTree.cpp
File metadata and controls
56 lines (47 loc) · 1.27 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
class Solution {
public:
struct SumCount {
int sum, count;
};
// Groupies.
vector<vector<int>> tree;
map<pair<int, int>, SumCount> cache;
vector<int> count, sum;
void calculateCount(const int target, const int caller) {
for (int i = 0; i < tree[target].size(); i++) {
const int node = tree[target][i];
if (node == caller) continue;
// Recurse count.
calculateCount(node, target);
count[target] += count[node];
sum[target] += sum[node] + count[node];
}
}
void calculateSum(const int target, const int caller) {
for (int i = 0; i < tree[target].size(); i++) {
const int node = tree[target][i];
if (node == caller) continue;
// Recurse sum.
sum[node] = sum[target] - count[node] + (count.size() - count[node]);
calculateSum(node, target);
}
}
vector<int> sumOfDistancesInTree(int n, vector<vector<int>>& edges) {
// Initialize groupies.
tree = vector<vector<int>>(n);
cache.clear();
count = vector<int>(n, 1);
sum = vector<int>(n, 0);
// Create graph.
for (int i = 0; i < edges.size(); i++) {
// Add graph connections.
tree[edges[i][0]].emplace_back(edges[i][1]);
tree[edges[i][1]].emplace_back(edges[i][0]);
}
// Calculate count.
calculateCount(0, -1);
// Calculate sum.
calculateSum(0, -1);
return sum;
}
};