zoukankan      html  css  js  c++  java
  • 思维题

    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
    Copy
    3
    1 1
    Output
    1
    Input
    Copy
    5
    1 2 2 2
    Output
    3
    Input
    Copy
    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 个结点,在每个时刻树上的苹果会向下脱落,但是落在同一节点的苹果数如果是偶数的话,则会全部烂掉,如果是奇数的话则会剩下一个,问所有苹果落至根节点后会剩下多少个。

    思路分析 : 计算出每层的苹果数量,对2取余,累加即可。

    代码示例 :

    const int maxn = 1e5+5;
    const double pi = acos(-1.0);
    const int inf = 0x3f3f3f3f;
    
    int a[maxn], cnt[maxn];
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        int n, x;
        
        cin >> n;
        a[1] = 1; cnt[1] = 1;
        for(int i = 2; i <= n; i++){
            scanf("%d", &x);
            a[i] = a[x] + 1;
            cnt[a[i]]++;
        }
        int ans = 0;
        for(int i = 1; i <= n; i++){
            if (cnt[i] % 2) ans++;
        }
        printf("%d
    ", ans);
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    在博客园里给图片加水印(canvas + drag)
    Chrome开发者工具使用指南
    《古剑奇谭3》千秋戏辅助工具(前端React制作)
    React中useEffect的源码解读
    关于为什么使用React新特性Hook的一些实践与浅见
    使用@babel/preset-typescript取代awesome-typescript-loader和ts-loader
    使用dva改造React旧项目的数据流方案
    在React旧项目中安装并使用TypeScript的实践
    安利一个绘制指引线的JS库leader-line
    微信小程序中悬浮窗功能的实现(主要探讨和解决在原生组件上的拖动)
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8510645.html
Copyright © 2011-2022 走看看