#include <bits/stdc++.h>
#define N 200005
using namespace std;
typedef long long ll;
int n, ini[N], fim[N], tempo;
ll sum[N], qtd[N], deep[N];
vector<int> grafo[N];
void dfs(int x, int p, ll d)
{
ini[x] = ++tempo;
deep[x] = d, sum[ini[x]] += d, qtd[ini[x]] ++;
for(auto v: grafo[x])
{
if(v == p) continue;
dfs(v, x, d + 1);
}
fim[x] = tempo;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n;
for(int i = 2, a; i <= n; i++)
{
cin>>a;
grafo[i].push_back(a);
grafo[a].push_back(i);
}
dfs(1, 1, 1LL);
for(int i = 1; i <= n; i++) sum[i] += sum[i - 1], qtd[i] += qtd[i - 1];
for(int x = 1; x <= n; x++) cout<<(ll)(sum[fim[x]] - sum[ini[x] - 1] - (deep[x] - 1)*(qtd[fim[x]] - qtd[ini[x] - 1]))<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5124 KB |
Output is correct |
2 |
Correct |
5 ms |
5196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5396 KB |
Output is correct |
2 |
Correct |
5 ms |
5396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5396 KB |
Output is correct |
2 |
Correct |
5 ms |
5396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
6596 KB |
Output is correct |
2 |
Correct |
14 ms |
6976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
9936 KB |
Output is correct |
2 |
Correct |
34 ms |
9996 KB |
Output is correct |
3 |
Correct |
32 ms |
11276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
18444 KB |
Output is correct |
2 |
Correct |
83 ms |
21516 KB |
Output is correct |
3 |
Correct |
87 ms |
34872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
126 ms |
34872 KB |
Output is correct |
2 |
Correct |
87 ms |
34872 KB |
Output is correct |
3 |
Correct |
86 ms |
34872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
34872 KB |
Output is correct |
2 |
Correct |
83 ms |
34872 KB |
Output is correct |
3 |
Correct |
84 ms |
34872 KB |
Output is correct |