#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi pair<int,int>
#define sz(x) (int)x.size()
const int N = 2e5+10;
vector<ll> adj[N],ans(N),cnt(N);
int n;
void dfs1(int u){
cnt[u] = 1;
for(auto v:adj[u]){
dfs1(v);
cnt[u] += cnt[v];
}
}
void dfs2(int u){
ans[u] += cnt[u];
for(auto v:adj[u]){
dfs2(v);
ans[u] += ans[v];
}
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1;i<n;i++){
int x;cin >> x;
x--;
adj[x].push_back(i);
}
/*
for(int i=0;i<n;i++){
cout << i << '\n';
for(auto v:adj[i]) cout << v << ' ';
cout << '\n';
}
*/
dfs1(0);
dfs2(0);
for(int i=0;i<n;i++){
cout << ans[i] << ' ';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8540 KB |
Output is correct |
2 |
Correct |
6 ms |
8808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
9564 KB |
Output is correct |
2 |
Correct |
14 ms |
9820 KB |
Output is correct |
3 |
Correct |
13 ms |
10864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
12276 KB |
Output is correct |
2 |
Correct |
34 ms |
14416 KB |
Output is correct |
3 |
Correct |
38 ms |
27228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
12380 KB |
Output is correct |
2 |
Correct |
36 ms |
13404 KB |
Output is correct |
3 |
Correct |
34 ms |
15444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
12208 KB |
Output is correct |
2 |
Correct |
37 ms |
13736 KB |
Output is correct |
3 |
Correct |
38 ms |
17236 KB |
Output is correct |