#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
using ll = long long;
const int mod = 1e9+7;
vector<vector<int>> adj;
vector<ll> mon, belo;
void dfs(int u, int par) {
if (!sz(adj[u])) {
mon[u] = 1;
return;
}
for (int v : adj[u]) {
dfs(v, u);
}
for (int v : adj[u]) {
mon[u] += mon[v];
belo[u] += belo[v] + 1;
}
mon[u] += belo[u] + 1;
}
void solve(int tc) {
int n;
cin >> n;
adj.resize(n);
mon.resize(n);
belo.resize(n);
vector<int> par(n);
for (int i = 1; i < n; ++i) {
cin >> par[i];
--par[i];
adj[par[i]].push_back(i);
}
dfs(0, -1);
for (auto &x : mon) cout << x << " ";
cout << '\n';
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int tc = 1;
//cin >> tc;
for (int i = 1; i <= tc; ++i) solve(i);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1644 KB |
Output is correct |
2 |
Correct |
8 ms |
2156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
5100 KB |
Output is correct |
2 |
Correct |
24 ms |
5484 KB |
Output is correct |
3 |
Correct |
24 ms |
6508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
13804 KB |
Output is correct |
2 |
Correct |
66 ms |
16364 KB |
Output is correct |
3 |
Correct |
68 ms |
27756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
13804 KB |
Output is correct |
2 |
Correct |
68 ms |
15212 KB |
Output is correct |
3 |
Correct |
63 ms |
17260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
13548 KB |
Output is correct |
2 |
Correct |
67 ms |
15468 KB |
Output is correct |
3 |
Correct |
63 ms |
19052 KB |
Output is correct |