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

來源:互聯網
上載者:User

標籤:more   stream   ext   head   final   any   output   star   contain   

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

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 pi-th inflorescence and pi?<?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 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 pa-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.

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

Input

First line of input contains 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 into which the apple from i-th inflorescence rolls down.

Output

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.

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 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.

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.

分析:

因為只有同一時間在同一地點的蘋果相碰會消失,那麼相碰一定只出現在同一層的蘋果之間,且在該層蘋果大於1的時候,一定會出現相碰。

所以我們可以將蘋果分層進行討論

如果該層的蘋果數為奇數,那麼無論經過怎樣的過程,到最後始終會剩一個。

如果為偶數,那麼到最後一定一個也不剩。

將每層得到的結果累加即可

#include <cstdio>#include <iostream>#include <algorithm>#include <vector>using namespace std;typedef long long LL;vector<int>V[100010];int num[100010];int maxx;void dfs(int id,int k){  num[k]++;  maxx=max(k,maxx);  for(int i=0;i<V[id].size();i++)   dfs(V[id][i],k+1);}int main(){    int n,x,ans;    cin>>n;    maxx=0;    ans=0;    for(int i=2;i<=n;i++)    {        cin>>x;        V[x].push_back(i);    }    dfs(1,1);    for(int i=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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.