简单树形dp uvaLive 4472

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<set>
#include<cmath>
using namespace std;
const int maxn=100002;
int t,n;
vector<int>g[maxn];
int dfs(int u,int fa)
{
    int i,k,l=g[u].size();
    multiset<int>p;

    for(i=0;i<l;i++)
    {
        int v=g[u][i];
        if(v==fa)continue;
        p.insert(dfs(v,u));
    }
    if(p.size()==0)return 1;
     int ret=0,j=0;
     k=ceil(l*0.01*t);
   while(j<k)
    {

        __typeof(p.begin()) w=p.begin();
        ret+=*w;
        p.erase(w);
        j++;
    } p.clear();
    return ret;
}
int main()
{
    while(scanf("%d%d",&n,&t),(n||t))
    {
        int i,j;
        for(i=0;i<=n;i++)g[i].clear();
        for(i=1;i<=n;i++)
        {
            cin>>j;
            g[j].push_back(i);
            //g[i].push_back(j);
        }
        cout<<dfs(0,-1)<<endl;
    }
    return 0;
}

你可能感兴趣的:(简单树形dp uvaLive 4472)