#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
using namespace std;
const int MAXN = 2 * 1e5 + 5;
int par[MAXN];
vector<int> adjList[MAXN];
long long subtree[MAXN], sums[MAXN];
void dfs(int src) {
for (auto i : adjList[src]) {
dfs(i);
subtree[src] += subtree[i];
sums[src] += subtree[i] + sums[i];
}
}
int main() {
int n;
cin >> n;
par[1] = 1;
subtree[1] = 0;
sums[1] = 1;
for (int i = 2; i <= n; i++) {
int temp;
cin >> temp;
par[i] = temp;
adjList[temp].push_back(i);
subtree[i] = 1;
sums[i] = 1;
}
dfs(1);
for (int i = 1; i <= n; i++) {
cout << sums[i];
if (i != n) {
cout << " ";
}
}
cout << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
5760 KB |
Output is correct |
2 |
Correct |
22 ms |
6144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
7672 KB |
Output is correct |
2 |
Correct |
61 ms |
8076 KB |
Output is correct |
3 |
Correct |
61 ms |
9080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
12580 KB |
Output is correct |
2 |
Correct |
164 ms |
14968 KB |
Output is correct |
3 |
Correct |
170 ms |
28024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
198 ms |
12792 KB |
Output is correct |
2 |
Correct |
171 ms |
13944 KB |
Output is correct |
3 |
Correct |
166 ms |
15992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
194 ms |
12408 KB |
Output is correct |
2 |
Correct |
169 ms |
14204 KB |
Output is correct |
3 |
Correct |
168 ms |
17912 KB |
Output is correct |