/*input
5
1 2 2 4
*/
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>adj[202020];
ll ans[202020];
void dfs(int i, ll &sz, ll &ret)
{
sz = 1;
ret = 0;
for (int j : adj[i])
{
ll x, y;
dfs(j, x, y);
sz += x;
ret += y;
}
ret += sz;
ans[i] = ret;
}
int main()
{
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 2; i <= n; i++)
{
int p;
cin >> p;
adj[p].push_back(i);
}
ll x, y;
dfs(1, x, y);
for (int i = 1; i <= n; i++)
cout << ans[i] << " ";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5140 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5760 KB |
Output is correct |
2 |
Correct |
14 ms |
6076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
7288 KB |
Output is correct |
2 |
Correct |
24 ms |
7680 KB |
Output is correct |
3 |
Correct |
25 ms |
8696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
11700 KB |
Output is correct |
2 |
Correct |
61 ms |
13916 KB |
Output is correct |
3 |
Correct |
64 ms |
24848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
11604 KB |
Output is correct |
2 |
Correct |
65 ms |
12840 KB |
Output is correct |
3 |
Correct |
62 ms |
14840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
114 ms |
11392 KB |
Output is correct |
2 |
Correct |
68 ms |
13176 KB |
Output is correct |
3 |
Correct |
64 ms |
16624 KB |
Output is correct |