codeforces 931 D. Peculiar apple-tree

Description

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 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 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 2 nd and 3 rd 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 2 nd inflorescence will roll down to 1 st (Arcady will collect it) and apples from 3 rd , 4 th , 5 th inflorescences will roll down to 2 nd . Two of them will annihilate and one not annihilated will roll down from 2 nd inflorescence to 1 st one in the next second and Arcady will collect it.


题意:一棵树上各个结点的苹果,每经过一秒会移动到父节点上;已经在根节点上的苹果会被收集。每当有两个苹果同时移动到同一个结点,他们就会消失。起先每个结点上都有一个苹果,问最后能在根结点处收集到多少个苹果。
思路:每两个苹果相遇会抵消,说明任何时间在任何节点上,都最多只有一个苹果。而由于所有的苹果在同步运动,所以只有同一层的苹果相互之间在抵消。也就是说,每当一层的苹果滚动到上一层,总数就会减少2的倍数,因此“滚动”是不改变某一层所含苹果总数的奇偶性的。所以,只有某一层含奇数个苹果时,它最终才会在根节点处贡献且只贡献一个苹果;而如果这一层含偶数个苹果,那么它一定在到达根节点时或之前就抵消完了,不会贡献苹果。
那么算法就是,统计各层的苹果数,有几层是奇数个苹果,就能收集到几个苹果。

#include <stdio.h>
const int maxn = 100005;

int fa[maxn] = { 0 };//记录父节点
int deep[maxn] = { 0 };//记录所在层数

int cnt[maxn] = { 0 };//记录每一层有几个苹果

//递归求结点i所在层数
int find(int i) {
    if (deep[i])return deep[i];
    else return deep[i]=(find(fa[i]) + 1);
}

int main() {
    int i = 1, n=0,temp=0;
    scanf("%d", &n);

    for (i = 2; i <= n; ++i) {
        scanf("%d", &fa[i]);
    }
    deep[1] = 1;
    cnt[1] = 1;
    for (i = 2; i <= n; ++i) {
        deep[i] = find(i);
        cnt[deep[i]]++;//在找完结点i的层数后顺便更新cnt数组
    }
    int ans = 0;
    for (i = 1; i <= n; ++i)
        ans += cnt[i] & 1;//奇数层贡献一个,偶数层贡献0个

    printf("%d",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值