#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define N +200500
#define MaxS N * N
#define M ll(1e9 + 7)
#define sz(x) (int)x.size()
//#define re return
#define oo ll(1e18)
#define el '\n'
#define pii pair <int, int>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less_equal <int> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef long double ld;
int d[N], siz[N], f[N], i, n, v;
vector <int> g[N];
void dfs(int v, int pr)
{
d[v] = d[pr] + 1;
f[v] = 0;
siz[v] = 1;
for (auto u : g[v])
{
if (u == pr) continue;
dfs(u, v);
siz[v] += siz[u];
f[v] += f[u];
}
f[v] += siz[v];
}
int main()
{
srand(time(0));
ios_base::sync_with_stdio(0);
iostream::sync_with_stdio(0);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
// in("input.txt");
// out("output.txt");
cin >> n;
for (i = 2; i <= n; i++)
{
cin >> v;
g[v].pb(i);
}
for (i = 1; i <= n; i++) sort(g[i].begin(), g[i].end());
d[0] = 0;
dfs(1, 0);
for (i = 1; i <= n; i++) cout << f[i] << " ";
}
// x ^ 2 + y ^ 2 = 1
// x * a_i + y * b_i
// a_i = -b_i * tan(alpha)
// a_i / -b_i = tan(alpha)
// alpha = atan(a_i / (-b_i))
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
5760 KB |
Output is correct |
2 |
Correct |
16 ms |
6144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
7272 KB |
Output is correct |
2 |
Correct |
29 ms |
7672 KB |
Output is correct |
3 |
Correct |
34 ms |
8568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
11368 KB |
Output is correct |
2 |
Incorrect |
75 ms |
13176 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
11256 KB |
Output is correct |
2 |
Correct |
77 ms |
12536 KB |
Output is correct |
3 |
Incorrect |
71 ms |
13944 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
11128 KB |
Output is correct |
2 |
Correct |
77 ms |
12664 KB |
Output is correct |
3 |
Incorrect |
72 ms |
15352 KB |
Output isn't correct |