930A.md (2918B)
1 Source: https://codeforces.com/problemset/problem/930/A 2 3 A. Peculiar apple-tree 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are *n* inflorescences, numbered from 1 to *n*. Inflorescence number 1 is situated near base of tree and any other inflorescence with number *i* (*i* > 1) is situated at the top of branch, which bottom is *p**i*-th inflorescence and *p**i* < *i*. 22 23 Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in *a*-th inflorescence gets to *p**a*-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. 24 25 Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. 26 27 Input 28 29 First line of input contains single integer number *n* (2 ≤ *n* ≤ 100 000) — number of inflorescences. 30 31 Second line of input contains sequence of *n* - 1 integer numbers *p*2, *p*3, ..., *p**n* (1 ≤ *p**i* < *i*), where *p**i* is number of inflorescence into which the apple from *i*-th inflorescence rolls down. 32 33 Output 34 35 Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. 36 37 Examples 38 39 Input 40 41 ``` 42 3 43 1 1 44 ``` 45 46 Output 47 48 ``` 49 1 50 ``` 51 52 Input 53 54 ``` 55 5 56 1 2 2 2 57 ``` 58 59 Output 60 61 ``` 62 3 63 ``` 64 65 Input 66 67 ``` 68 18 69 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 70 ``` 71 72 Output 73 74 ``` 75 4 76 ``` 77 78 Note 79 80 In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. 81 82 In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.