21275_WealthyFamily

2022-5-16 18:19| 发布者: Hocassian| 查看: 22| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-3-8950589762299-Problem List-采集的数据-后羿采集器.html

Pro.ID

21275

Title

Wealthy Family

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21275

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    While studying the history of royal families, you want to know how wealthy each family is. While you have various 'net worth' figures for each individual throughout history, this is complicated by double counting caused by inheritance. One way to estimate the wealth of a family is to sum up the net worth of a set of k people such that no one in the set is an ancestor of another in the set. The wealth of the family is the maximum sum achievable over all such sets of k people. Since historical records contain only the net worth of male family members, the family tree is a simple tree in which every male has exactly one father and a non-negative number of sons. You may assume that there is one person who is an ancestor of all other family members.

    输入

    The input consists of a number of cases. Each case starts with a line containing two integers separated by a space: N (1 ≤ N ≤ 150,000), the number of people in the family, and k (1 ≤ k ≤ 300), the size of the set. The next N lines contain two non-negative integers separated by a space: the parent number and the net worth of person i (1 ≤ i ≤ N). Each person is identified by a number between 1 and N, inclusive. There is exactly one person who has no parent in the historical records, and this will be indicated with a parent number of 0. The net worths are given in millions and each family member has a net worth of at least 1 million and at most 1 billion.

    输出

    Description

    While studying the history of royal families, you want to know how wealthy each family is. While you have various 'net worth' figures for each individual throughout history, this is complicated by double counting caused by inheritance. One way to estimate the wealth of a family is to sum up the net worth of a set of k people such that no one in the set is an ancestor of another in the set. The wealth of the family is the maximum sum achievable over all such sets of k people. Since historical records contain only the net worth of male family members, the family tree is a simple tree in which every male has exactly one father and a non-negative number of sons. You may assume that there is one person who is an ancestor of all other family members.

    Input

    The input consists of a number of cases. Each case starts with a line containing two integers separated by a space: N (1 ≤ N ≤ 150,000), the number of people in the family, and k (1 ≤ k ≤ 300), the size of the set. The next N lines contain two non-negative integers separated by a space: the parent number and the net worth of person i (1 ≤ i ≤ N). Each person is identified by a number between 1 and N, inclusive. There is exactly one person who has no parent in the historical records, and this will be indicated with a parent number of 0. The net worths are given in millions and each family member has a net worth of at least 1 million and at most 1 billion.

    Output

    For each case, print the maximum sum (in millions) achievable over all sets of k people satisfying the constraints given above. If it is impossible to choose a set of k people without violating the constraints, print 'impossible' instead.

    Sample Input

    5 3
    0 10
    1 15
    1 25
    1 35
    4 45
    3 3
    0 10
    1 10
    2 10

    Sample Output

    85
    impossible

    Source

    样例输入

    5 3
    0 10
    1 15
    1 25
    1 35
    4 45
    3 3
    0 10
    1 10
    2 10

    样例输出

    85
    impossible

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部