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

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.

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

题意:一棵树上1~N个节点,1为基部节点,给定从2~N的节点的每个节点连接的下一个节点。现在每个节点产生一个苹果,每过一秒向下滚动一个节点,如果有偶数个的苹果同时汇入同一节点则碰撞消失,问总共能收集多少苹果。
因为最后的汇点唯一,所以同一层的苹果必然会在下面的某一个节点相遇,那么只需要从下向上做一次bfs,再统计每一层苹果数量,偶数则答案加一。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define pn printf("\n")
using namespace std;
typedef long long ll;

const int maxn = 1e5+7;
int dep = 0;
struct edge{
    int to, next;
}e[maxn], _e[maxn];
int head[maxn], tot;
int n, cur[maxn], cnt[maxn];

void init()
{
    tot = 0;
    memset(head, -1, sizeof head);
}

void addedge(int u,int v)
{
    e[tot].to = v;
    e[tot].next = head[u];
    head[u] = tot ++;
}

void bfs()
{
    queue <int> que;
    cur[1] = 0;
    que.push(1);
    
    while(!que.empty())
    {
        int a = que.front();
        que.pop();
        dep = max(dep, cur[a]);
        cnt[cur[a]]++;
        
        for(int i=head[a]; i!=-1; i=e[i].next)
        {
            int v = e[i].to;
            cur[v] = cur[a] + 1;
            que.push(v);
        }
    }
}

int solve()
{
    int res = 0;
    for(int i=0;i<=dep;i++)
        if(cnt[i]&1)
            res ++;
    return res;
}

int main()
{
    init();
    scanf("%d",&n);
    int x;
    for(int i=0;i<n-1;i++)
    {
        scanf("%d",&x);
        addedge(x, i+2);
    }
    bfs();

    printf("%d\n", solve());
}

 

转载于:https://www.cnblogs.com/HazelNut/p/8622046.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值