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)