-
Notifications
You must be signed in to change notification settings - Fork 3
/
834.cpp
130 lines (123 loc) · 4.7 KB
/
834.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
class Solution {
public:
int N;
void dfs1(vector<vector<int>>& adjacency, int cur, int pre, vector<int>& count, vector<int>& res) {
for (auto& neighbor : adjacency[cur]) {
if (neighbor == pre) continue;
dfs1(adjacency, neighbor, cur, count, res);
count[cur] += count[neighbor];
res[cur] += res[neighbor] + count[neighbor];
}
++count[cur];
}
void dfs2(vector<vector<int>>& adjacency, int cur, int pre, vector<int>& count, vector<int>& res) {
for (auto& neighbor : adjacency[cur]) {
if (neighbor == pre) continue;
res[neighbor] = res[cur] - count[neighbor] + (N - count[neighbor]);
dfs2(adjacency, neighbor, cur, count, res);
}
}
vector<int> sumOfDistancesInTree(int n, vector<vector<int>>& edges) {
N = n;
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> res(n, 0);
vector<int> count(n, 0);
dfs1(adjacency, 0, -1, count, res);
dfs2(adjacency, 0, -1, count, res);
return res;
}
};
class Solution {
public:
unordered_map<string, int> edge2cnt;
int N;
int dfs(int node, vector<vector<int>>& adjacency, vector<bool>& visited) {
visited[node] = true;
int total = 1;
for (auto& neighbor : adjacency[node]) {
if (visited[neighbor]) continue;
int cnt = dfs(neighbor, adjacency, visited);
edge2cnt[to_string(node) + "#" + to_string(neighbor)] = cnt;
edge2cnt[to_string(neighbor) + "#" + to_string(node)] = N - cnt;
total += cnt;
}
return total;
}
void dfsEdge(int node, vector<vector<int>>& adjacency, vector<bool>& visited, int cnt, int& total) {
visited[node] = true;
total += cnt;
for (auto& neighbor : adjacency[node]) {
if (visited[neighbor]) continue;
dfsEdge(neighbor, adjacency, visited, cnt + 1, total);
}
}
void dfsAnswer(int node, vector<vector<int>>& adjacency, vector<bool>& visited, int previous, vector<int>& res) {
visited[node] = true;
for (auto& neighbor : adjacency[node]) {
if (visited[neighbor]) continue;
int next = previous - edge2cnt[to_string(node) + "#" + to_string(neighbor)] + edge2cnt[to_string(neighbor) + "#" + to_string(node)];
res[neighbor] = next;
dfsAnswer(neighbor, adjacency, visited, next, res);
}
}
void resetVisit(vector<bool>& visited) {
for (int i = 0; i < N; ++i) visited[i] = false;
}
vector<int> sumOfDistancesInTree(int n, vector<vector<int>>& edges) {
N = n;
vector<vector<int>> adjacency(n);
for (auto& edge : edges) {
adjacency[edge[0]].push_back(edge[1]);
adjacency[edge[1]].push_back(edge[0]);
}
vector<bool> visited(n, false);
dfs(0, adjacency, visited);
vector<int> res(n, 0);
resetVisit(visited);
int total = 0;
dfsEdge(0, adjacency, visited, 0, total);
res[0] = total;
resetVisit(visited);
dfsAnswer(0, adjacency, visited, total, res);
return res;
}
};
class Solution {
public:
int dfs(int node, int parent, int depth, vector<int>& childrens, int& sum, vector<vector<int>>& adjacency) {
sum += depth;
int childrenCnt = 0;
for (auto& neighbor : adjacency[node]) {
if (neighbor == parent) continue;
childrenCnt += dfs(neighbor, node, depth + 1, childrens, sum, adjacency);
}
childrens[node] = childrenCnt;
return childrenCnt + 1;
}
void dfs2(int n, int sum, int node, int parent, vector<int>& childrens, vector<vector<int>>& adjacency, vector<int>& answer) {
answer[node] = sum;
for (auto& neighbor : adjacency[node]) {
if (neighbor == parent) continue;
int childrenCnt = childrens[neighbor] + 1;
int leftCnt = n - childrenCnt;
dfs2(n, sum - childrenCnt + leftCnt, neighbor, node, childrens, adjacency, answer);
}
}
vector<int> sumOfDistancesInTree(int n, vector<vector<int>>& edges) {
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> childrens(n, 0);
vector<int> res(n, 0);
int sum = 0;
dfs(0, -1, 0, childrens, sum, adjacency);
dfs2(n, sum, 0, -1, childrens, adjacency, res);
return res;
}
};