#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
#define MEM(a, b) memset(a, (b), sizeof(a))
#define ALL(c) (c).begin(),(c).end()
#define sz(a) ((int)(a.size()))
#define ll long long
#define LINF (ll)1e18
#define INF (int)1e9
#define MINF 0x3F3F3F3F
#define pb push_back
#define fs first
#define sc second
#define mp make_pair
#define MOD 1000000007
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define MAX 200005
#define watch(x) cerr<<#x<<" = "<<(x)<<endl;
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template<class T> using indexed_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int n;
ll arr[MAX], c[MAX];
vector<int> adj[MAX];
void dfs1(int s, int p) {
c[s] = 1;
for(auto u:adj[s]) {
if(u==p) continue;
dfs1(u, s);
c[s] += c[u];
}
}
void dfs2(int s, int p) {
for(auto u:adj[s]) {
if(u==p) continue;
dfs2(u, s);
c[s] += c[u];
}
}
int main() {
cin >> n;
for(int i=2;i<=n;i++) cin >> arr[i];
for(int i=2;i<=n;i++) {adj[arr[i]].push_back(i);adj[i].push_back(arr[i]);}
dfs1(1, 0); dfs2(1, 0);
for(int i=1;i<=n;i++) cout << c[i] << " ";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4988 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5116 KB |
Output is correct |
2 |
Correct |
7 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
6008 KB |
Output is correct |
2 |
Correct |
25 ms |
6388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
8592 KB |
Output is correct |
2 |
Correct |
70 ms |
8596 KB |
Output is correct |
3 |
Correct |
65 ms |
9848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
14968 KB |
Output is correct |
2 |
Correct |
180 ms |
16924 KB |
Output is correct |
3 |
Correct |
183 ms |
30200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
14976 KB |
Output is correct |
2 |
Correct |
187 ms |
15664 KB |
Output is correct |
3 |
Correct |
169 ms |
18040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
14840 KB |
Output is correct |
2 |
Correct |
190 ms |
16060 KB |
Output is correct |
3 |
Correct |
177 ms |
20332 KB |
Output is correct |