#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
#define mp make_pair
#define f first
#define s second
#define pb push_back
#define lb lower_bound
#define ins insert
#define sz(x) (int)(x).size()
int N, D;
struct Points{
int lazy; //add to the distance
set<pi> all_points; //distance, number
Points(){
lazy = 0;
// all_points.ins(mp(0, 1));
}
};
void print(Points* a){
cout << "printing all_points: " << "\n";
for(auto u: a->all_points){
u.f+=a->lazy;
cout << u.f << " " << u.s << "\n";
}
}
void insertPoint(Points* a, pi b){
// cout << "insertPoint: " << "\n";
// cout << "a: " << "\n";
// print(a);
// cout << "b: " << b.f << " " << b.s << "\n";
b.f-=a->lazy;
if(sz(a->all_points) == 0){
a->all_points.ins(b);
//print(a);
return;
}
auto it = a->all_points.lb(mp(b.f, 0));
if(it != a->all_points.end() && it->s >= b.s){
//print(a);
return;
}
it = a->all_points.lb(mp(b.f+1, 0));
while(it != a->all_points.begin()){
auto to_remove = prev(it);
if(to_remove->s <= b.s){
a->all_points.erase(to_remove);
}
else{
break;
}
}
a->all_points.ins(b);
// print(a);
}
void comb(Points* a, Points* b){ //insert b stuff into a
if(sz(a->all_points) < sz(b->all_points)){
swap(a->lazy, b->lazy);
swap(a->all_points, b->all_points);
}
//insert b stuff into a
vpi to_add;
for(auto u: b->all_points){
u.f+=b->lazy; //u.f is actual distance value now
to_add.pb(u);
int needed_dist = D-u.f-a->lazy;
auto it = a->all_points.lb(mp(needed_dist, 0));
if(it != a->all_points.end()){
to_add.pb(mp(min(u.f, (it->f)+a->lazy), u.s+it->s));
}
}
for(auto u: to_add){
insertPoint(a, u);
}
}
const int mx = 200005;
vi adj[mx];
Points* dp[mx];
void genDP(int node, int p = -1){
// cout << "node = " << node << "\n";
// cout.flush();
// cout << "dp[" << node << "]->all_points: " << "\n";
// print(dp[node]);
for(auto u: adj[node]){
if(u == p) continue;
genDP(u, node);
dp[u]->lazy++;
comb(dp[node], dp[u]);
// cout << "u: " << u << "\n";
// cout.flush();
// cout << "dp[" << node << "]->all_points: " << "\n";
// print(dp[node]);
}
// cout << "dp[" << node << "]->all_points: " << "\n";
// print(dp[node]);
Points* emp = new Points();
emp->all_points.ins(mp(0, 1));
comb(dp[node], emp);
// cout << "dp[" << node << "]->all_points: " << "\n";
// print(dp[node]);
// cout << "node done: " << node << "\n";
// cout.flush();
}
int main(){
cin.tie(0)->sync_with_stdio(0);
for(int i = 0; i < mx; i++){
dp[i] = new Points();
}
cin >> N >> D;
for(int i = 1; i < N; i++){
int xi;
cin >> xi;
adj[xi].pb(i);
adj[i].pb(xi);
}
genDP(0);
assert(sz(dp[0]->all_points));
cout << dp[0]->all_points.begin()->s << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19020 KB |
Output is correct |
2 |
Correct |
16 ms |
19020 KB |
Output is correct |
3 |
Correct |
17 ms |
19040 KB |
Output is correct |
4 |
Correct |
14 ms |
19020 KB |
Output is correct |
5 |
Correct |
17 ms |
19084 KB |
Output is correct |
6 |
Correct |
15 ms |
19004 KB |
Output is correct |
7 |
Correct |
15 ms |
19048 KB |
Output is correct |
8 |
Correct |
13 ms |
18996 KB |
Output is correct |
9 |
Correct |
16 ms |
19020 KB |
Output is correct |
10 |
Correct |
17 ms |
19008 KB |
Output is correct |
11 |
Correct |
17 ms |
19060 KB |
Output is correct |
12 |
Correct |
17 ms |
19096 KB |
Output is correct |
13 |
Correct |
18 ms |
19024 KB |
Output is correct |
14 |
Correct |
17 ms |
19012 KB |
Output is correct |
15 |
Correct |
16 ms |
19020 KB |
Output is correct |
16 |
Correct |
17 ms |
19096 KB |
Output is correct |
17 |
Correct |
15 ms |
19100 KB |
Output is correct |
18 |
Correct |
16 ms |
18988 KB |
Output is correct |
19 |
Correct |
15 ms |
19020 KB |
Output is correct |
20 |
Correct |
14 ms |
19072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19020 KB |
Output is correct |
2 |
Correct |
16 ms |
19020 KB |
Output is correct |
3 |
Correct |
17 ms |
19040 KB |
Output is correct |
4 |
Correct |
14 ms |
19020 KB |
Output is correct |
5 |
Correct |
17 ms |
19084 KB |
Output is correct |
6 |
Correct |
15 ms |
19004 KB |
Output is correct |
7 |
Correct |
15 ms |
19048 KB |
Output is correct |
8 |
Correct |
13 ms |
18996 KB |
Output is correct |
9 |
Correct |
16 ms |
19020 KB |
Output is correct |
10 |
Correct |
17 ms |
19008 KB |
Output is correct |
11 |
Correct |
17 ms |
19060 KB |
Output is correct |
12 |
Correct |
17 ms |
19096 KB |
Output is correct |
13 |
Correct |
18 ms |
19024 KB |
Output is correct |
14 |
Correct |
17 ms |
19012 KB |
Output is correct |
15 |
Correct |
16 ms |
19020 KB |
Output is correct |
16 |
Correct |
17 ms |
19096 KB |
Output is correct |
17 |
Correct |
15 ms |
19100 KB |
Output is correct |
18 |
Correct |
16 ms |
18988 KB |
Output is correct |
19 |
Correct |
15 ms |
19020 KB |
Output is correct |
20 |
Correct |
14 ms |
19072 KB |
Output is correct |
21 |
Correct |
15 ms |
19496 KB |
Output is correct |
22 |
Correct |
14 ms |
19148 KB |
Output is correct |
23 |
Correct |
18 ms |
19100 KB |
Output is correct |
24 |
Correct |
17 ms |
19148 KB |
Output is correct |
25 |
Correct |
16 ms |
19284 KB |
Output is correct |
26 |
Correct |
14 ms |
19216 KB |
Output is correct |
27 |
Correct |
14 ms |
19244 KB |
Output is correct |
28 |
Correct |
15 ms |
19332 KB |
Output is correct |
29 |
Correct |
16 ms |
19288 KB |
Output is correct |
30 |
Correct |
16 ms |
19268 KB |
Output is correct |
31 |
Correct |
17 ms |
19220 KB |
Output is correct |
32 |
Correct |
16 ms |
19312 KB |
Output is correct |
33 |
Correct |
15 ms |
19236 KB |
Output is correct |
34 |
Correct |
16 ms |
19332 KB |
Output is correct |
35 |
Correct |
17 ms |
19276 KB |
Output is correct |
36 |
Correct |
18 ms |
19284 KB |
Output is correct |
37 |
Correct |
18 ms |
19276 KB |
Output is correct |
38 |
Correct |
19 ms |
19260 KB |
Output is correct |
39 |
Correct |
16 ms |
19276 KB |
Output is correct |
40 |
Correct |
16 ms |
19508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19020 KB |
Output is correct |
2 |
Correct |
16 ms |
19020 KB |
Output is correct |
3 |
Correct |
17 ms |
19040 KB |
Output is correct |
4 |
Correct |
14 ms |
19020 KB |
Output is correct |
5 |
Correct |
17 ms |
19084 KB |
Output is correct |
6 |
Correct |
15 ms |
19004 KB |
Output is correct |
7 |
Correct |
15 ms |
19048 KB |
Output is correct |
8 |
Correct |
13 ms |
18996 KB |
Output is correct |
9 |
Correct |
16 ms |
19020 KB |
Output is correct |
10 |
Correct |
17 ms |
19008 KB |
Output is correct |
11 |
Correct |
17 ms |
19060 KB |
Output is correct |
12 |
Correct |
17 ms |
19096 KB |
Output is correct |
13 |
Correct |
18 ms |
19024 KB |
Output is correct |
14 |
Correct |
17 ms |
19012 KB |
Output is correct |
15 |
Correct |
16 ms |
19020 KB |
Output is correct |
16 |
Correct |
17 ms |
19096 KB |
Output is correct |
17 |
Correct |
15 ms |
19100 KB |
Output is correct |
18 |
Correct |
16 ms |
18988 KB |
Output is correct |
19 |
Correct |
15 ms |
19020 KB |
Output is correct |
20 |
Correct |
14 ms |
19072 KB |
Output is correct |
21 |
Correct |
15 ms |
19496 KB |
Output is correct |
22 |
Correct |
14 ms |
19148 KB |
Output is correct |
23 |
Correct |
18 ms |
19100 KB |
Output is correct |
24 |
Correct |
17 ms |
19148 KB |
Output is correct |
25 |
Correct |
16 ms |
19284 KB |
Output is correct |
26 |
Correct |
14 ms |
19216 KB |
Output is correct |
27 |
Correct |
14 ms |
19244 KB |
Output is correct |
28 |
Correct |
15 ms |
19332 KB |
Output is correct |
29 |
Correct |
16 ms |
19288 KB |
Output is correct |
30 |
Correct |
16 ms |
19268 KB |
Output is correct |
31 |
Correct |
17 ms |
19220 KB |
Output is correct |
32 |
Correct |
16 ms |
19312 KB |
Output is correct |
33 |
Correct |
15 ms |
19236 KB |
Output is correct |
34 |
Correct |
16 ms |
19332 KB |
Output is correct |
35 |
Correct |
17 ms |
19276 KB |
Output is correct |
36 |
Correct |
18 ms |
19284 KB |
Output is correct |
37 |
Correct |
18 ms |
19276 KB |
Output is correct |
38 |
Correct |
19 ms |
19260 KB |
Output is correct |
39 |
Correct |
16 ms |
19276 KB |
Output is correct |
40 |
Correct |
16 ms |
19508 KB |
Output is correct |
41 |
Correct |
120 ms |
49072 KB |
Output is correct |
42 |
Correct |
112 ms |
37860 KB |
Output is correct |
43 |
Incorrect |
114 ms |
34824 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |