#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, p[N], sz[N];
long long dp[N];
vector<int> g[N];
void dfs(int v){
sz[v] = 1;
for (int u : g[v]){
dfs(u);
sz[v] += sz[u];
dp[v] += dp[u];
}
dp[v] += sz[v];
}
int main(){
cin >> n;
for (int i = 2; i <= n; i ++){
cin >> p[i];
g[p[i]].push_back(i);
}
dfs(1);
for (int i = 1; i <= n; i ++)
cout << dp[i] << " ";
cout << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7004 KB |
Output is correct |
2 |
Correct |
8 ms |
7512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
8540 KB |
Output is correct |
2 |
Correct |
21 ms |
8792 KB |
Output is correct |
3 |
Correct |
22 ms |
10072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
11860 KB |
Output is correct |
2 |
Correct |
60 ms |
14416 KB |
Output is correct |
3 |
Correct |
66 ms |
28756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
11720 KB |
Output is correct |
2 |
Correct |
60 ms |
13272 KB |
Output is correct |
3 |
Correct |
57 ms |
15892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
11852 KB |
Output is correct |
2 |
Correct |
61 ms |
13652 KB |
Output is correct |
3 |
Correct |
72 ms |
18304 KB |
Output is correct |