书上给出了思路和DP部分的代码,其他的就很简单了。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 
 | #include<cstdio>#include<vector>
 #include<algorithm>
 using namespace std;
 const int maxn=100010;
 int n,t;
 vector<int> sons[maxn];
 int dp(int u){
 if(sons[u].empty()) return 1;
 int k=(int)sons[u].size();
 vector<int> d;
 for(int i=0;i<k;++i)
 d.push_back(dp(sons[u][i]));
 sort(d.begin(),d.end());
 int c=(k*t-1)/100+1;
 int ans=0;
 for(int i=0;i<c;++i)
 ans+=d[i];
 return ans;
 }
 int main(){
 while(~scanf("%d%d",&n,&t)){
 if(!n&&!t) break;
 for(int i=0;i<=n;++i) sons[i].clear();
 for(int i=1;i<=n;++i){
 int k;
 scanf("%d",&k);
 sons[k].push_back(i);
 }
 printf("%d\n",dp(0));
 }
 return 0;
 }
 
 | 
** 本文迁移自我的CSDN博客,格式可能有所偏差。 **