This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |