Stone Problem

Problem Description
  There is an new stone game, played on an arbitrary general tree. The goal is to put one stone on the root of tree observing the following rules: 
    1.people can only put stone on a leaf.
    2.At each step of the game, the player can put one stone on any empty leaf.
    3.When T percent of the children of a node p has stone, the node p can automatically has a stone.
  Given the tree's structure and the parameter T, you have to find out the minimum number of stones that have to be putted on leaves in order to make the root get a stone.
Input
  There are several test cases. The input for each test case is given in exactly two lines. The first line contains two integers N and T ( 1<=N<=10^5 , 1<=T<=100), separated by a single space. N indicates the number of nodes of the tree (not counting the root) and T is the parameter described above. Each of the nodes is identified by an integer between 1 and N. The root is identified by the number 0. The second line contains a list of integers separated by single spaces. The integer Bi, at position i on this list (starting from 1), indicates the identification of the direct father of node i (0<=Bi<=i - 1).
  The last test case is followed by a line containing two zeros separated by a single space.
Output
For each test case output a single line containing a single integer with the minimum number of needed stones.
Sample Input
3 100 
0 0 0 
3 50 
0 0 0 
14 60 
0 0 1 1 2 2 2 5 7 5 7 5 7 5 
0 0
Sample Output


5

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
vector <int> son[100005];//存储某结点的儿子结点


int n,t;//结点数n,百分率t
int dfs(int note){//搜索
if(son[note].size() == 0)//搜索到叶子结点时返回1
return 1;
vector <int> v;
for(int i = 0; i<(int)son[note].size(); i ++)//递推搜索每个儿子结点
v.push_back(dfs(son[note][i]));
sort(v.begin(), v.end());//将每个儿子结点的递归结果递增排序,以保证结果为最小值
int p = ceil((double)t/100*(int)son[note].size());//按照百分率确定每层应取的结点数
int res = 0;
for(int i = 0; i < p; i ++)
res += v[i];//取前p个结点的值
return res;
}
int main(){
int temp;
while(cin>>n>>t&&(n!=0||t!=0))
{
for(int i=0; i <=n; i++)
son[i].clear();//清空结点关系记录
for(int i = 1; i <= n; i ++)
{
cin>>temp;
son[temp].push_back(i);//输入各个结点的关系

}
cout<<dfs(0)<<endl;
}
return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值