#include <bits/stdc++.h>
#define N 200005
using namespace std;
typedef long long ll;
int n, ini[N], fim[N], tempo, deep[N], qtd[N];
ll sum[N];
vector<int> grafo[N];
void dfs(int x, int p, int 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, 1);
for(int i = 1; i <= 2*n; i++) sum[i] += sum[i - 1], qtd[i] += qtd[i - 1];
for(int x = 1; x <= n; x++) cout<<sum[fim[x]] - sum[ini[x] - 1] - (ll)(deep[x] - 1)*(ll)(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 |
5092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5200 KB |
Output is correct |
2 |
Correct |
5 ms |
5252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5316 KB |
Output is correct |
2 |
Correct |
5 ms |
5352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5356 KB |
Output is correct |
2 |
Correct |
5 ms |
5364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
6780 KB |
Output is correct |
2 |
Correct |
13 ms |
7408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
11032 KB |
Output is correct |
2 |
Correct |
31 ms |
11300 KB |
Output is correct |
3 |
Correct |
31 ms |
12980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
133 ms |
22492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
133 ms |
23496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
138 ms |
24388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |