-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2049.cpp
More file actions
49 lines (41 loc) · 1.14 KB
/
Copy path2049.cpp
File metadata and controls
49 lines (41 loc) · 1.14 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
class Solution {
public:
int countHighestScoreNodes(const vector<int> &parents) const {
const int n = size(parents);
vector<int> count(n, 0);
for (int i = 1; i < n; i++) {
count[parents[i]]++;
}
queue<int> q;
for (int i = 1; i < n; i++) {
if (!count[i]) q.push(i);
}
vector<int> below(n, 1);
while (q.front()) {
const int root = q.front();
q.pop();
const int parent = parents[root];
if (!--count[parent]) q.push(parent);
below[parent] += below[root];
}
vector<int> above(n, 0);
for (int i = 1; i < n; i++) {
above[i] = below[0] - below[i];
}
vector<long long> score(n, 1);
for (int i = 1; i < n; i++) {
score[parents[i]] *= below[i];
score[i] *= above[i];
}
int res = 0;
long long maxi = 0;
for (const auto n : score) {
if (n == maxi) res++;
if (n > maxi) {
maxi = n;
res = 1;
}
}
return res;
}
};