#include<bits/stdc++.h>
#define ll long long
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define int int64_t
using namespace std;
typedef pair<int, int> pii;
const int N = (int)2e5 + 10;
int dep[N], n, d, p[N], ans = 0;
int in[N], out[N], T, u, cur, par, rem;
vector<int> adj[N];
vector<pii> v;
struct TSegment {
vector<int> l, h, st, lz;
int n, inf;
void init(int _n, int d) {
n = ++_n; inf = 2 * d; st.resize(n << 2, inf);
l.resize(n << 2), h.resize(n << 2), lz.resize(n << 2, inf);
--n; build(1, 1, n);
}
void build(int x, int low, int high) {
l[x] = low, h[x] = high;
if(l[x] == h[x]) return;
int mid = (l[x] + h[x]) >> 1;
build(x << 1, low, mid);
build(x << 1 | 1, mid + 1, high);
}
void change(int x, int val) {st[x] = min(st[x], val); lz[x] = min(lz[x], val);}
void down(int x) {
if(lz[x] == inf || l[x] == h[x]) return;
change(x << 1, lz[x]), change(x << 1 | 1, lz[x]);
lz[x] = inf;
}
void upd(int x, int low, int high, int val) {
down(x);
if(l[x] > high || h[x] < low) return;
if(l[x] >= low && h[x] <= high) {
st[x] = min(st[x], val);
lz[x] = min(lz[x], val);
return;
}
upd(x << 1, low, high, val); upd(x << 1 | 1, low, high, val);
st[x] = min(st[x << 1], st[x << 1 | 1]);
}
int get(int x, int low, int high) {
down(x);
if(l[x] > high || h[x] < low) return inf;
if(l[x] >= low && h[x] <= high) return st[x];
return min(get(x << 1, low, high), get(x << 1 | 1, low, high));
}
} it;
void DFS(int u) {
in[u] = ++T;
for(int v: adj[u]) DFS(v);
out[u] = T;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define FileName "test"
if(fopen(FileName".inp", "r")) {
freopen(FileName".inp", "r", stdin);
freopen(FileName".out", "w", stdout);
}
cin >> n >> d; v.pb(0, 0);
for(int i = 1; i < n; ++i) {
cin >> p[i];
adj[p[i]].pb(i);
dep[i] = dep[p[i]] + 1;
v.pb(dep[i], i);
}
sort(v.begin(), v.end(), greater<pii>());
DFS(0); it.init(n, d);
for(pii& node: v) {
u = node.se; //cout << u << ' ' << it.get(1, in[u], in[u]) + dep[u] << '\n';
if(it.get(1, in[u], in[u]) + dep[u] >= d) {
++ans; cur = u;
while(1) {
it.upd(1, in[cur], out[cur], dep[u] - 2 * dep[cur]);
if(cur == 0) break;
cur = p[cur];
}
}
}
cout << ans;
}
Compilation message
catinatree.cpp: In function 'int32_t main()':
catinatree.cpp:70:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FileName".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:71:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FileName".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
7 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5112 KB |
Output is correct |
14 |
Correct |
8 ms |
5116 KB |
Output is correct |
15 |
Correct |
8 ms |
4984 KB |
Output is correct |
16 |
Correct |
8 ms |
4984 KB |
Output is correct |
17 |
Correct |
8 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
4984 KB |
Output is correct |
19 |
Correct |
8 ms |
5112 KB |
Output is correct |
20 |
Correct |
8 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
7 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5112 KB |
Output is correct |
14 |
Correct |
8 ms |
5116 KB |
Output is correct |
15 |
Correct |
8 ms |
4984 KB |
Output is correct |
16 |
Correct |
8 ms |
4984 KB |
Output is correct |
17 |
Correct |
8 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
4984 KB |
Output is correct |
19 |
Correct |
8 ms |
5112 KB |
Output is correct |
20 |
Correct |
8 ms |
4984 KB |
Output is correct |
21 |
Correct |
9 ms |
5368 KB |
Output is correct |
22 |
Correct |
9 ms |
5240 KB |
Output is correct |
23 |
Correct |
9 ms |
5112 KB |
Output is correct |
24 |
Correct |
8 ms |
5112 KB |
Output is correct |
25 |
Correct |
10 ms |
5240 KB |
Output is correct |
26 |
Correct |
9 ms |
5240 KB |
Output is correct |
27 |
Correct |
9 ms |
5240 KB |
Output is correct |
28 |
Correct |
9 ms |
5368 KB |
Output is correct |
29 |
Correct |
10 ms |
5368 KB |
Output is correct |
30 |
Correct |
9 ms |
5368 KB |
Output is correct |
31 |
Correct |
9 ms |
5372 KB |
Output is correct |
32 |
Correct |
9 ms |
5364 KB |
Output is correct |
33 |
Correct |
8 ms |
5368 KB |
Output is correct |
34 |
Correct |
8 ms |
5368 KB |
Output is correct |
35 |
Correct |
9 ms |
5368 KB |
Output is correct |
36 |
Correct |
9 ms |
5368 KB |
Output is correct |
37 |
Correct |
9 ms |
5368 KB |
Output is correct |
38 |
Correct |
9 ms |
5368 KB |
Output is correct |
39 |
Correct |
9 ms |
5368 KB |
Output is correct |
40 |
Correct |
50 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
7 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5112 KB |
Output is correct |
14 |
Correct |
8 ms |
5116 KB |
Output is correct |
15 |
Correct |
8 ms |
4984 KB |
Output is correct |
16 |
Correct |
8 ms |
4984 KB |
Output is correct |
17 |
Correct |
8 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
4984 KB |
Output is correct |
19 |
Correct |
8 ms |
5112 KB |
Output is correct |
20 |
Correct |
8 ms |
4984 KB |
Output is correct |
21 |
Correct |
9 ms |
5368 KB |
Output is correct |
22 |
Correct |
9 ms |
5240 KB |
Output is correct |
23 |
Correct |
9 ms |
5112 KB |
Output is correct |
24 |
Correct |
8 ms |
5112 KB |
Output is correct |
25 |
Correct |
10 ms |
5240 KB |
Output is correct |
26 |
Correct |
9 ms |
5240 KB |
Output is correct |
27 |
Correct |
9 ms |
5240 KB |
Output is correct |
28 |
Correct |
9 ms |
5368 KB |
Output is correct |
29 |
Correct |
10 ms |
5368 KB |
Output is correct |
30 |
Correct |
9 ms |
5368 KB |
Output is correct |
31 |
Correct |
9 ms |
5372 KB |
Output is correct |
32 |
Correct |
9 ms |
5364 KB |
Output is correct |
33 |
Correct |
8 ms |
5368 KB |
Output is correct |
34 |
Correct |
8 ms |
5368 KB |
Output is correct |
35 |
Correct |
9 ms |
5368 KB |
Output is correct |
36 |
Correct |
9 ms |
5368 KB |
Output is correct |
37 |
Correct |
9 ms |
5368 KB |
Output is correct |
38 |
Correct |
9 ms |
5368 KB |
Output is correct |
39 |
Correct |
9 ms |
5368 KB |
Output is correct |
40 |
Correct |
50 ms |
5496 KB |
Output is correct |
41 |
Correct |
240 ms |
42844 KB |
Output is correct |
42 |
Correct |
906 ms |
24804 KB |
Output is correct |
43 |
Correct |
119 ms |
24676 KB |
Output is correct |
44 |
Correct |
109 ms |
24676 KB |
Output is correct |
45 |
Correct |
109 ms |
24804 KB |
Output is correct |
46 |
Execution timed out |
1082 ms |
44348 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |