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;
}
if(graf[v].size()==1 and v!=o){
dp[v]={1,0};
return;
}
sort(curr.begin(),curr.end());
dp[v].fi=0;
int g=curr.size()-1;
for(int i=0;i<curr.size()-1;i++){
if(curr[i].fi+curr[i+1].fi+2>=k){
g=i;
break;
}
}
dp[v].fi=aktsum-g,dp[v].se=curr[g].se+1;
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);
}
if(n==1){
cout<<1<<"\n";
return;
}
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:39:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i=0;i<curr.size()-1;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... |