ASC (1) C (tree DP)

Source: Internet
Author: User

ASC (1) C (tree DP)
New Year Bonus GrantSpecial JudgeTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Others) submitStatisticNext ProblemProblem Description All programmers of Mocrosoft software company are organized in a strict subordination hierarchy. every programmer has exactly one chief, doesn't Bill Hates who is also the head of the company and has no chief.
Due to the celebration of the new 2003 year, chief accountant of Mocrosoft decided to pay a New Year Bonus Grant of 1000 dollars to some programmers. however being extremely concerned of the company wealth she wowould like to design the least possible amount of money for these grants. on the other hand she didn't want to be accused of being too greedy or of giving preferences to some programmers. to do this, she developed the following scheme of grants appointment:
? Each programmer may either assign a grant to one of his subordinates or have a grant assigned to him by his chief or none of the above.
? No programmer can simultaneously receive a grant and assign a grant to one of his subordinates.
? No programmer can assign a grant to more than one of his subordinates
The scheme seemed to be designed perfectly-nobody wocould like to assign a grant to anybody since in this case he himself wocould not receive money. but programmers somehow discovered the plan of chief accountant and decided to make a trick to get the most money possible and share them fairly afterwards. the idea was to make such grant assignments that the total amount of grant money saved Ed is maximum possible.
You were selected to write the program which will find the optimal grants appointment. input The first line of the input file contains integer N-the number of programmers in Mocrosoft company (2 ≤n ≤500 000 ). each programmer is assigned his unique identifier-integer number ranging from 1 to N. bill Hates has number 1 and each programmer has the number greater then the number of his chief. the Second line of the input file contains N? 1 integers, I-th of which being the number of the chief of the worker whose number is (I + 1 ). output On the first line of the output file print the maximum possible amount of money workers can get. on the second line output the numbers of programmers that will receive grant in ascending order. sample Input

41 1 2
Sample Output
20003 4

Give a tree and find the greatest match. Each vertex must match the father, a child, or a child.
Idea: n = 500000. The Bipartite Graph Matching Algorithm obviously cannot be implemented.
Because it is a tree, the decision tree can be DP
Dp [u] [0] indicates that the vertex does not match the parent, and the maximum number of matched child trees
Dp [u] [1] indicates that the vertex matches its parent, and the maximum number of matched child trees
It is easy to think that dp [u] [1] can only be accumulated by dp [v] [0], where v is u, because u cannot match any other child after matching his father
Dp [u] [0] can be initialized to dp [u] [1], indicating that u does not match the parent or the child.
Then use dp [v] [1] to maximize dp [u] [0].
Finally, you must output a specific scheme and print the path directly by the hierarchy of the tree.

Related Article

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.