Ultraviolet A 12186 Another Crisis tree DP, 12186 crisis

Source: Internet
Author: User

Ultraviolet A 12186 Another Crisis tree DP, 12186 crisis


Tree DP


Another Crisis
Time Limit:3000 MS   Memory Limit:Unknown   64bit IO Format:% Lld & % llu

Submit Status

Description

A couple of years ago, a new world wide crisis started, leaving has people with economy problems. Some workers of a particle company are trying to ask for an increase in their salaries.

The company has a strict hierarchy, in which each employee has exactly one direct boss, with the exception of the owner of the company that has no boss. employees that are not bosses of any other employee are called workers. the rest of the employees and the owner are called bosses.

To ask for a salary increase, a worker shocould file a petition to his direct boss. of course, each boss is encouraged to try to make their subordinates happy with their current income, making the company's profit as high as possible. however, when at leastTPercent of its direct subordinates have filed a petition, that boss will be pressured and have no choice but to file a petition himself to his own direct boss. each boss files at most 1 petition to his own direct boss, regardless on how much of his subordinates filed him a petition. A boss only accounts his direct subordinates (the ones that filed him a petition and the ones that didn't) to calculate the pressure percentage.

Note that a boss can have both workers and bosses as direct subordinates at the same time. such a boss may receive petitions from both kinds of employees, and each direct subordinate, regardless of its kind, will be accounted as 1 when checking the pressure percentage.

When a petition file gets all the way up to the owner of the company, all salaries are increased. the workers 'Union is desperately trying to make that happen, so they need to convince submit workers to file a petition to their direct boss.

Given the company's hierarchy and the parameterT, You have to find out the minimum number of workers that have to file a petition in order to make the owner receive a petition.

Input

There are several test cases. The input for each test case is given in exactly two lines. The first line contains two integersNAndT(1N105, 1T100), separated by a single space.NIndicates the number of employees of the company (not counting the owner) andTIs the parameter described above. Each of the employees is identified by an integer between 1 andN. The owner is identified by the number 0. The second line contains a list of integers separated by single spaces. The integerBI, at positionIOn this list (starting from 1), indicates the identification of the direct boss of employeeI(0BII-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 workers that need to file a petition in order to get the owner of the company to receive a petition.

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

3 2 5

Source

Root: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim): Graph: Special Graphs (Others): Tree
Root: aoapc ii: Beginning Algorithm Contests (Second Edition) (Rujia Liu): Chapter 9. Dynamic Programming: Examples

Submit Status


/*************************************** * ******** Author: CKbossCreated Time: October 16, Sunday, File Name: UVA12186.cpp *************************************** * ********/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <cmath> # include <cstdlib> # include <vector> # include <queue> # include <set> # include <map> using namespace std; const int maxn = 110 000; int n, T; vector <int> vi [maxn]; int dp [maxn]; void init () {for (int I = 0; I <= n + 10; I ++) {vi [I]. clear (); dp [I] =-1 ;}} int dfs (int now) {if (dp [now]! =-1) return dp [now]; int sz = vi [now]. size (); if (sz = 0) return dp [now] = 1; vector <int> temp; for (int I = 0; I <sz; I ++) {int to = vi [now] [I]; temp. push_back (dfs (to);} sort (temp. begin (), temp. end (); int goal = (int) ceil (1. * sz * T/100); int ret = 0; for (int I = 0; I <goal; I ++) {ret + = temp [I];} return dp [now] = ret;} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt ", "w", stdout); while (scanf ("% d", & n, & T )! = EOF) {if (n = 0 & T = 0) break; init (); for (int I = 1; I <= n; I ++) {int x; scanf ("% d", & x); vi [x]. push_back (I);} printf ("% d \ n", dfs (0);} return 0 ;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.