1 2 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 34 35 36
| #include <bits/stdc++.h> #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define per(i,a,b) for(int i=(a);i>=(b);--i) using namespace std; const int N=1e5+5; int n,t; int target[N],pre[N],last[N],tot=0; void add(int u,int v) { target[++tot] = v; pre[tot] = last[u]; last[u] = tot; } int dfs(int u) { if(!last[u]) return 1; int tmp[N],cnt=0; for(int ptr=last[u];ptr;ptr=pre[ptr]) { tmp[++cnt]=dfs(target[ptr]); } sort(tmp+1,tmp+cnt+1); int ned = (cnt*t-1)/100+1,dp=0; rep(i,1,ned) dp+=tmp[i]; return dp; } int main(){ while(~scanf("%d%d",&n,&t)&&n&&t) { tot=0; memset(last,0,sizeof(last)); rep(i,1,n) { int x; scanf("%d",&x); add(x+1,i+1); } printf("%d\n",dfs(1)); } return 0; }
|