File tree Expand file tree Collapse file tree 4 files changed +215
-3
lines changed
solution/2000-2099/2049.Count Nodes With the Highest Score Expand file tree Collapse file tree 4 files changed +215
-3
lines changed Original file line number Diff line number Diff line change 58
58
59
59
<!-- 这里可写通用的实现逻辑 -->
60
60
61
- 第一步可以将 ` parents ` 数组转为相对好处理的邻接矩阵
61
+ 第一步可以将 ` parents ` 数组转为相对好处理的邻接矩阵。
62
62
63
- 接下来,观察样例 1 中的 ` Removed 2 ` ,删除一个节点可能产生若干子树,或者整棵树除掉以该节点为根的子树后剩下的部分
63
+ 接下来,观察样例 1 中的 ` Removed 2 ` ,删除一个节点可能产生若干子树,或者整棵树除掉以该节点为根的子树后剩下的部分。
64
64
65
- 总结出规律后,递归处理即可
65
+ 总结出规律后,递归处理即可。
66
66
67
67
<!-- tabs:start -->
68
68
@@ -187,6 +187,80 @@ function countHighestScoreNodes(parents: number[]): number {
187
187
}
188
188
```
189
189
190
+ ### ** C++**
191
+
192
+ ``` cpp
193
+ class Solution {
194
+ public:
195
+ int ans;
196
+ long long maxScore;
197
+ int n;
198
+
199
+ int countHighestScoreNodes(vector<int>& parents) {
200
+ ans = 0;
201
+ maxScore = 0;
202
+ n = parents.size();
203
+ unordered_map<int, vector<int>> g;
204
+ for (int i = 1; i < n; ++i) g[parents[i]].push_back(i);
205
+ dfs(0, g);
206
+ return ans;
207
+ }
208
+
209
+ int dfs (int u, unordered_map<int, vector<int >>& g) {
210
+ int size = 1;
211
+ long long score = 1;
212
+ for (int v : g[ u] )
213
+ {
214
+ int t = dfs(v, g);
215
+ size += t;
216
+ score * = t;
217
+ }
218
+ if (u > 0) score * = (n - size);
219
+ if (score > maxScore)
220
+ {
221
+ maxScore = score;
222
+ ans = 1;
223
+ }
224
+ else if (score == maxScore) ++ans;
225
+ return size;
226
+ }
227
+ };
228
+ ```
229
+
230
+ ### **Go**
231
+
232
+ ```go
233
+ func countHighestScoreNodes(parents []int) int {
234
+ n := len(parents)
235
+ g := make([][]int, n)
236
+ for i := 1; i < n; i++ {
237
+ p := parents[i]
238
+ g[p] = append(g[p], i)
239
+ }
240
+ maxScore, ans := 0, 0
241
+ var dfs func(int) int
242
+ dfs = func(u int) int {
243
+ size, score := 1, 1
244
+ for _, v := range g[u] {
245
+ t := dfs(v)
246
+ size += t
247
+ score *= t
248
+ }
249
+ if u > 0 {
250
+ score *= n - size
251
+ }
252
+ if score > maxScore {
253
+ maxScore, ans = score, 1
254
+ } else if score == maxScore {
255
+ ans++
256
+ }
257
+ return size
258
+ }
259
+ dfs(0)
260
+ return ans
261
+ }
262
+ ```
263
+
190
264
### ** ...**
191
265
192
266
```
Original file line number Diff line number Diff line change @@ -169,6 +169,80 @@ function countHighestScoreNodes(parents: number[]): number {
169
169
}
170
170
```
171
171
172
+ ### ** C++**
173
+
174
+ ``` cpp
175
+ class Solution {
176
+ public:
177
+ int ans;
178
+ long long maxScore;
179
+ int n;
180
+
181
+ int countHighestScoreNodes(vector<int>& parents) {
182
+ ans = 0;
183
+ maxScore = 0;
184
+ n = parents.size();
185
+ unordered_map<int, vector<int>> g;
186
+ for (int i = 1; i < n; ++i) g[parents[i]].push_back(i);
187
+ dfs(0, g);
188
+ return ans;
189
+ }
190
+
191
+ int dfs (int u, unordered_map<int, vector<int >>& g) {
192
+ int size = 1;
193
+ long long score = 1;
194
+ for (int v : g[ u] )
195
+ {
196
+ int t = dfs(v, g);
197
+ size += t;
198
+ score * = t;
199
+ }
200
+ if (u > 0) score * = (n - size);
201
+ if (score > maxScore)
202
+ {
203
+ maxScore = score;
204
+ ans = 1;
205
+ }
206
+ else if (score == maxScore) ++ans;
207
+ return size;
208
+ }
209
+ };
210
+ ```
211
+
212
+ ### **Go**
213
+
214
+ ```go
215
+ func countHighestScoreNodes(parents []int) int {
216
+ n := len(parents)
217
+ g := make([][]int, n)
218
+ for i := 1; i < n; i++ {
219
+ p := parents[i]
220
+ g[p] = append(g[p], i)
221
+ }
222
+ maxScore, ans := 0, 0
223
+ var dfs func(int) int
224
+ dfs = func(u int) int {
225
+ size, score := 1, 1
226
+ for _, v := range g[u] {
227
+ t := dfs(v)
228
+ size += t
229
+ score *= t
230
+ }
231
+ if u > 0 {
232
+ score *= n - size
233
+ }
234
+ if score > maxScore {
235
+ maxScore, ans = score, 1
236
+ } else if score == maxScore {
237
+ ans++
238
+ }
239
+ return size
240
+ }
241
+ dfs(0)
242
+ return ans
243
+ }
244
+ ```
245
+
172
246
### ** ...**
173
247
174
248
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int ans;
4
+ long long maxScore;
5
+ int n;
6
+
7
+ int countHighestScoreNodes (vector<int >& parents) {
8
+ ans = 0 ;
9
+ maxScore = 0 ;
10
+ n = parents.size ();
11
+ unordered_map<int , vector<int >> g;
12
+ for (int i = 1 ; i < n; ++i) g[parents[i]].push_back (i);
13
+ dfs (0 , g);
14
+ return ans;
15
+ }
16
+
17
+ int dfs (int u, unordered_map<int , vector<int >>& g) {
18
+ int size = 1 ;
19
+ long long score = 1 ;
20
+ for (int v : g[u])
21
+ {
22
+ int t = dfs (v, g);
23
+ size += t;
24
+ score *= t;
25
+ }
26
+ if (u > 0 ) score *= (n - size);
27
+ if (score > maxScore)
28
+ {
29
+ maxScore = score;
30
+ ans = 1 ;
31
+ }
32
+ else if (score == maxScore) ++ans;
33
+ return size;
34
+ }
35
+ };
Original file line number Diff line number Diff line change
1
+ func countHighestScoreNodes (parents []int ) int {
2
+ n := len (parents )
3
+ g := make ([][]int , n )
4
+ for i := 1 ; i < n ; i ++ {
5
+ p := parents [i ]
6
+ g [p ] = append (g [p ], i )
7
+ }
8
+ maxScore , ans := 0 , 0
9
+ var dfs func (int ) int
10
+ dfs = func (u int ) int {
11
+ size , score := 1 , 1
12
+ for _ , v := range g [u ] {
13
+ t := dfs (v )
14
+ size += t
15
+ score *= t
16
+ }
17
+ if u > 0 {
18
+ score *= n - size
19
+ }
20
+ if score > maxScore {
21
+ maxScore , ans = score , 1
22
+ } else if score == maxScore {
23
+ ans ++
24
+ }
25
+ return size
26
+ }
27
+ dfs (0 )
28
+ return ans
29
+ }
You can’t perform that action at this time.
0 commit comments