#include <bits/stdc++.h>
using namespace std;
#define sp << " " <<
#define int long long
#define vi vector<int>
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
#define pii pair<int,int>
#define pb push_back
const int N = 5e5+1;
const int inf = 1e9+1;
const int MOD = 1e9+7;
vi edges[N],sz(N),dp(N);
void dfs(int node) {
for (auto it : edges[node]) dfs(it);
int ans = 1;
sz[node] = 1;
for (auto it : edges[node]) {
ans+=dp[it]+sz[it];
sz[node]+=sz[it];
}
dp[node] = ans;
}
void solve() {
int n;
cin >> n;
for (int i=2;i<=n;i++) {
int p;
cin >> p;
edges[p].pb(i);
}
dfs(1);
F(i,n) cout << dp[i] << " ";
cout << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int t = 1;
//cin >> t;
F(i,t) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19804 KB |
Output is correct |
2 |
Correct |
5 ms |
19804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19896 KB |
Output is correct |
2 |
Correct |
5 ms |
19812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19804 KB |
Output is correct |
2 |
Correct |
5 ms |
20056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
20316 KB |
Output is correct |
2 |
Correct |
10 ms |
20828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
21684 KB |
Output is correct |
2 |
Correct |
18 ms |
22104 KB |
Output is correct |
3 |
Correct |
17 ms |
23132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
25312 KB |
Output is correct |
2 |
Correct |
43 ms |
27392 KB |
Output is correct |
3 |
Correct |
44 ms |
38740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
25344 KB |
Output is correct |
2 |
Correct |
41 ms |
26452 KB |
Output is correct |
3 |
Correct |
40 ms |
28500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
25112 KB |
Output is correct |
2 |
Correct |
39 ms |
26748 KB |
Output is correct |
3 |
Correct |
40 ms |
30288 KB |
Output is correct |