Sgu 195 new year bonus grant [simple greedy]

Source: Internet
Author: User
Link: http://acm.sgu.ru/problem.php? Contest = 0 & problem = 195 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 26733 # Problem/e195. New Year bonus granttime limit per test: 1.5 sec.
Memory limit per test: 65536 kbinput: Standard
Output: Standard




All programmersMocrosoftSoftware 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 accountantMocrosoftDecided 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 amount 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 inMocrosoftCompany (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 integer, 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 test (s)
Input

4 1 1 2 output 
2000 

3 4

I understood the meaning of the question quickly after the competition last night and did not write it out. I just flipped through the cxb PPT last year (I didn't understand it at the time) and copied my ideas and questions. It's a pity that the cxb kids shoes are abandoned, but they have their own aspirations. orz
• Mocrosoft software has n employees. Everyone except the boss (billhates) has his own boss. Now the Chinese New Year is over, and the boss plans to give the employees a bonus. To minimize expenditures, there are three rules:

· Eachprogrammer may either assign a grant to one of his subordinates or have a grantassigned to him by his chief or none of the above.

 I,Each employee can arrange his/her subordinates to receive a bonus and wait for his/her superiors to give it to him. You can also do nothing. (That is

After he has assigned a bonus to his subordinates, he will not be able to receive it)

· Noprogrammer can simultaneously receive a grant and assign a grant to one of hissubordinates.

2. No programmer can receive the bonus from his superiors at the same time and arrange the bonus for his subordinates.

(That is to say, after he arranges a bonus for his subordinates, he will not be entitled to the bonus !)

· Noprogrammer can assign a grant to more than one of his subordinates

3. Each programmer can only assign a bonus to one of his subordinates (if he has one.


Note: The number entered each time is the number of the father of the next vertex. Therefore, the input in the question is a tree from top to bottom. You can find it from bottom to top when programming.

Algorithm: greedy
Ideas:

The problem is converted to a tree coloring:

(1) Each node has at most one son stained

(2) If a node is stained, all its sons cannot be stained.

(That is, if a node is stained, his father, brother, and son cannot be stained)

Sample Analysis diagram:
Code:
Accepted 6695 KB 234 MS Visual maxcompute Studio C ++ 2010 763 B

# Include <stdio. h> # include <string. h> const int maxn = 500000 + 10; int P [maxn]; // Record Father int vis [maxn]; // mark whether money is allocated to int ans [maxn]; // record the employee ID int main () {int N; while (scanf ("% d", & N )! = EOF) {int sum = 0; For (INT I = 2; I <= N; I ++) {scanf ("% d ", & P [I]);} memset (VIS, 0, sizeof (VIS); // initialize for (INT I = N; I> 1; I --) // find {If (! Vis [I] &! Vis [p [I]) // if you do not receive money, and your father and brother do not receive the money {vis [I] = 1; vis [p [I] = 1; ans [Sum ++] = I; // cent} printf ("% d \ n", sum * 1000 ); for (INT I = sum-1; I> = 0; I --) {if (I = (Sum-1) printf ("% d ", ans [I]); else printf ("% d", ANS [I]);} printf ("\ n");} 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.