#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];
int 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 |
4992 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 |
18 ms |
5760 KB |
Output is correct |
2 |
Correct |
22 ms |
6144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
7544 KB |
Output is correct |
2 |
Correct |
59 ms |
8056 KB |
Output is correct |
3 |
Correct |
58 ms |
8952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
12280 KB |
Output is correct |
2 |
Incorrect |
163 ms |
14584 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
12152 KB |
Output is correct |
2 |
Correct |
173 ms |
13688 KB |
Output is correct |
3 |
Incorrect |
164 ms |
15608 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
12024 KB |
Output is correct |
2 |
Correct |
170 ms |
13944 KB |
Output is correct |
3 |
Incorrect |
164 ms |
17784 KB |
Output isn't correct |