#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=2e5+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
vi graf[N];
pair<int,int> dp[N];
int n,k;
void dfs(int v,int o){
vector<pair<int,int> >curr;
int aktsum=0;
for(auto u:graf[v]){
if(u==o) continue;
dfs(u,v);
curr.push_back({dp[u].se,dp[u].fi});
aktsum+=dp[u].fi;
}
sort(curr.begin(),curr.end());
auto bs=[&](int pocz,int kon,int x){
int sr,res=pocz;
while(pocz<=kon){
sr=(pocz+kon)>>1;
if(curr[sr].fi+x+2<k) res=sr,pocz=sr+1;
else kon=sr-1;
}
return res;
};
dp[v].fi=0;
for(int i=0;i<curr.size();i++){
pair<int,int> akt;
akt.fi=aktsum-i-(bs(i,curr.size()-1,curr[i].fi)-i);
akt.se=curr[i].fi+1;
if(akt.fi>dp[v].fi or (akt.fi==dp[v].fi and akt.se<dp[v].se)) dp[v]=akt;
}
for(auto u:graf[v]){
if(u==o) continue;
if(dp[u].se!=k-1) aktsum--;
}
aktsum++;
if(aktsum>dp[v].fi) dp[v]={aktsum,0};
}
void solve(){
cin>>n>>k;
for(int a,i=2;i<=n;i++){
cin>>a;
a++;
graf[i].push_back(a),graf[a].push_back(i);
}
dfs(1,1);
cout<<dp[1].fi<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
Compilation message
catinatree.cpp: In function 'void dfs(int, int)':
catinatree.cpp:43:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i=0;i<curr.size();i++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |