Codeforces 931D:Peculiar apple-tree

D. Peculiar apple-tree

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard 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 \(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 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.

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

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.

Examples

input

3
1 1

output

1

input

5
1 2 2 2

output

3

input

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.

题意

给你一棵树,有\(n\)个节点,每个节点有一个苹果,在苹果成熟后,苹果会落下来,如果有两个苹果落到了同一个节点,那么这两个苹果会消失,问最后会有多少个苹果落到根节点

思路

读题读了一年,发现是个傻逼题,嘤嘤嘤~
根据输入建好图,dfs求出每个节点的深度,然后算出各个深度有多少节点,如果是偶数个,那么最后落到根节点的苹果数一定是\(0\),反之为\(1\)

代码

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int a[maxn];
vector<int>edge[maxn];
int hei[maxn];
int num[maxn];
// 处理每个点的深度
void dfs(int father,int son)
{
    hei[son]=hei[father]+1;
    for(auto v:edge[son])
    {
        if(v!=son)
            dfs(son,v);
    }
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("/home/wzy/in.txt", "r", stdin);
        freopen("/home/wzy/out.txt", "w", stdout);
        srand((unsigned int)time(NULL));
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        cin>>a[i];
        edge[a[i]].push_back(i);
    }
    dfs(0,1);
    int ans=0;
    map<int,int>mp;
    for(int i=1;i<=n;i++)
        mp[hei[i]]++;
    for(auto it:mp)
        ans+=(it.second&1);
    cout<<ans<<endl;
    #ifndef ONLINE_JUDGE
        cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
    #endif
    return 0;
}
上一篇:Java1变量数据类型和运算符


下一篇:NYOJ201-作业题-(dp)