Acdream 1212 (greedy)

Source: Internet
Author: User

Test instructions: There are n individuals, from 1 to N, the bigger the number the lower the position, and then give the 2nd to nth person's boss number, everyone can have a bunch of little brother but only one boss. The end of the bonus, everyone can get 1000 yuan from the boss or to a younger brother 1000 yuan, ask everyone can send bonuses and the maximum is how much, those people get the bonus.
The puzzle: Go straight backwards, mark the person and his boss as a group, see a few groups.

#include <stdio.h>#include <string.h>ConstintN =500005;intPa[n], N, flag[n];intMain () { while(SCANF ("%d", &n) = =1) { for(inti =0; I <= N; i++) Flag[i] =0; for(inti =2; I <= N; i++) scanf ("%d", &pa[i]);intres =0; for(inti = n; I >=2; i--) {if(!flag[i] &&!flag[pa[i]) {Flag[i] =2; Flag[pa[i]] =1;            res++; }        }printf("%d\ n", res * +);inttemp =0; for(inti =2; I <= N; i++)if(Flag[i] = =2) {if(!temp) {printf("%d", i); temp =1; }Else                    printf(" %d", i); }printf("\ n"); }return 0;}

Acdream 1212 (greedy)

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.