• 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("
    ")
    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
    ", solve());
    }
  • 相关阅读:
    目录
    DRF的分页
    Django Rest Framework 视图和路由
    爬虫基本原理
    C# System.Threading.Timer的使用
    C# Task的使用
    C# 线程池的使用
    C# 异步委托回调函数使用
    C#异步委托等待句柄的使用
    C# 异步委托的使用
  • 原文地址:https://www.cnblogs.com/HazelNut/p/8622046.html
Copyright © 2020-2023  润新知