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;
#define ll long long int
#define pi pair<int,int>
#define sz(x) (int)x.size()
const int N = 2e5+10;
vector<ll> adj[N],ans(N),cnt(N);
int n;
void dfs1(int u){
cnt[u] = 1;
for(auto v:adj[u]){
dfs1(v);
cnt[u] += cnt[v];
}
}
void dfs2(int u){
ans[u] += cnt[u];
for(auto v:adj[u]){
dfs2(v);
ans[u] += ans[v];
}
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1;i<n;i++){
int x;cin >> x;
x--;
adj[x].push_back(i);
}
/*
for(int i=0;i<n;i++){
cout << i << '\n';
for(auto v:adj[i]) cout << v << ' ';
cout << '\n';
}
*/
dfs1(0);
dfs2(0);
for(int i=0;i<n;i++){
cout << ans[i] << ' ';
}
return 0;
}
# | 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... |