#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 200005;
const int mod = 1e9 + 7;
int n, p[MAXN], dep[MAXN], cnt[MAXN];
vector<int> gph[MAXN];
int sz[MAXN];
int din[MAXN], dout[MAXN], chn[MAXN], piv;
void hld(int x){
din[x] = ++piv;
if(sz(gph[x])){
chn[gph[x][0]] = chn[x];
hld(gph[x][0]);
for(int i = 1; i < sz(gph[x]); i++){
chn[gph[x][i]] = gph[x][i];
hld(gph[x][i]);
}
}
dout[x] = piv;
}
pi forward(int x){
int ret = 0;
while(~p[x] && cnt[x] == cnt[p[x]]){
x = p[x];
ret++;
}
return pi(x, ret);
}
void add_until(int s, int e, int x){
while(s != e){
cnt[s] += x;
s = p[s];
}
if(~s) cnt[s] += x;
}
int main(){
scanf("%d",&n);
p[0] = -1;
for(int i = 1; i <= n; i++){
scanf("%d",&p[i]);
dep[i] = dep[p[i]] + 1;
if(dep[i] % 2) cnt[i]++;
gph[p[i]].push_back(i);
}
for(int i = n; i; i--){
sz[i]++;
if(~p[i]) sz[p[i]] += sz[i];
sort(all(gph[i]), [&](const int &a, const int &b){
return sz[a] > sz[b];
});
}
hld(0);
for(int i = 1; i <= n; i++){
int ans = 0;
if(dep[i] % 2){
int j = i;
if(cnt[p[j]] == 0){
tie(j, ans) = forward(p[j]);
ans++;
}
add_until(p[i], p[j], 1);
}
else{
int j = i;
if(cnt[p[j]] == 1){
tie(j, ans) = forward(p[j]);
ans++;
}
add_until(p[i], p[j], -1);
}
printf("%d\n", ans);
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Main.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d",&p[i]);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
5 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
5 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
7 ms |
5452 KB |
Output is correct |
9 |
Correct |
218 ms |
6208 KB |
Output is correct |
10 |
Correct |
7 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
6 ms |
5452 KB |
Output is correct |
13 |
Correct |
15 ms |
5628 KB |
Output is correct |
14 |
Correct |
40 ms |
5452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
5 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
7 ms |
5452 KB |
Output is correct |
9 |
Correct |
218 ms |
6208 KB |
Output is correct |
10 |
Correct |
7 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
6 ms |
5452 KB |
Output is correct |
13 |
Correct |
15 ms |
5628 KB |
Output is correct |
14 |
Correct |
40 ms |
5452 KB |
Output is correct |
15 |
Correct |
47 ms |
9388 KB |
Output is correct |
16 |
Execution timed out |
1084 ms |
16636 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
14020 KB |
Output is correct |
2 |
Correct |
64 ms |
13636 KB |
Output is correct |
3 |
Correct |
40 ms |
10616 KB |
Output is correct |
4 |
Correct |
82 ms |
14272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
5 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
7 ms |
5452 KB |
Output is correct |
9 |
Correct |
218 ms |
6208 KB |
Output is correct |
10 |
Correct |
7 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
6 ms |
5452 KB |
Output is correct |
13 |
Correct |
15 ms |
5628 KB |
Output is correct |
14 |
Correct |
40 ms |
5452 KB |
Output is correct |
15 |
Correct |
47 ms |
9388 KB |
Output is correct |
16 |
Execution timed out |
1084 ms |
16636 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |