This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
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 (stderr)
catinatree.cpp: In function 'void dfs(int, int)':
catinatree.cpp:44: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]
44 | for(int i=0;i<curr.size();i++){
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |