Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round) d. Peculiar Apple-tree

Source: Internet
Author: User

D. Peculiar apple-treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard OUTPU T

In Arcady's garden there grows a peculiar apple-tree the fruits one time per year. Its peculiarity can is explained in following way:there is n inflorescences, numbered FR Om 1 to n . Inflorescence number 1 is situated near base of the tree and any other inflorescence with number i ( i ? >?1) is situated at the top of branch, which bottom is Span class= "Tex-span" > p i -th inflorescence and P i ? <? i .

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 the tree. Each second-apples, except ones in first inflorescence simultaneously roll-down one branch closer to tree base, e.g. a Pple in a-th inflorescence gets to pa-th inflorescence. Apples that end-in first inflorescence is gathered by Arcady in exactly the same moment. Second peculiarity of this tree was that once, and apples is in same inflorescence they annihilate . This happens with each pair of apples, e.g. if there is 5 apples in same inflorescence in same time, only one would no t be annihilated and if there is 8 apples, all apples would be annihilated. Thus, there can be is no more than one Apple in each inflorescence in each moment of time.

Help Arcady with counting number of apples he'll be able to collect from first inflorescence during one harvest.

Input

First line of input contains a single integer number n (2?≤? n? ≤?100?000)-number of inflorescences.

Second line of input contains sequence of n?-? 1 integer numbers p2,? P3,?...,? Pn (1?≤? Pi? <? I), where pi is number of inflorescence to which the apple from I- Th inflorescence rolls down.

Output

Single line of output should contain one integer number:amount of apples that Arcady would be able to collect from first I Nflorescence during one harvest.

Examplesinputcopy
3
1 1
Output
1
Inputcopy
5
1 2 2 2
Output
3
Inputcopy
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
Output
4
Note

In first example Arcady is able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences would roll down and annihilate, and Arcady won ' t is able to C Ollect them.

In the second example Arcady would be a able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence would roll down to 1st (Arcady would collect it) and apples from 3rd, c3>4th, 5th inflorescences to 2nd. them would annihilate and one not annihilated would roll down from 2-nd inflorescence to 1st one in the next Second and Arcady would collect it.

Analysis:

Because only the same time at the same place the apple collide will disappear, then the collision must only appear in the same layer of apples, and in this layer of apples greater than 1, will be bound to collide.

So we can discuss the Apple layering

If the number of apples in this layer is odd, then no matter what the process is, there will always be one left at a time.

If it is an even number, then there must be no one left at the end.

Accumulate the results from each layer

#include <cstdio>#include<iostream>#include<algorithm>#include<vector>using namespaceStd;typedefLong LongLl;vector<int>v[100010];intnum[100010];intMaxx;voidDfsintIdintk) {Num[k]++; Maxx=Max (K,maxx);  for(intI=0; I<v[id].size (); i++) DFS (v[id][i],k+1);}intMain () {intN,x,ans; CIN>>N; Maxx=0; Ans=0;  for(intI=2; i<=n;i++) {cin>>x;    V[x].push_back (i); } DFS (1,1);  for(intI=1; i<=maxx;i++)    {      if(num[i]%2==1) ans++; } cout<<ans<<Endl; return 0;}

Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round) d. Peculiar Apple-tree

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.