This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, p[N], sz[N];
long long dp[N];
vector<int> g[N];
void dfs(int v){
sz[v] = 1;
for (int u : g[v]){
dfs(u);
sz[v] += sz[u];
dp[v] += dp[u];
}
dp[v] += sz[v];
}
int main(){
cin >> n;
for (int i = 2; i <= n; i ++){
cin >> p[i];
g[p[i]].push_back(i);
}
dfs(1);
for (int i = 1; i <= n; i ++)
cout << dp[i] << " ";
cout << endl;
}
# | 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... |