Codeforces 931.D 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 pi-th inflorescence and pi < i.ios

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.app

Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.ui

Input

First line of input contains single integer number n (2 ≤ n ≤ 100 000)  — number of inflorescences.this

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.spa

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.code

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.blog

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.ip

題目大意:n個蘋果往根上掉,第i個分支的蘋果通過1s後會掉到第pi個分支,若是一個分支在某一個時刻有偶數個蘋果,這些蘋果全都會消失,不然保留下一個,當蘋果掉到1號點就被視爲撿到,問最多能撿到多少個蘋果.ci

分析:這題不要想複雜了......get

   把這棵樹給構造出來,深度爲i的蘋果須要跳i次才能跳到根節點,它們必然會在根節點相遇. 那麼對於每個深度,數一下有多少蘋果,若是有奇數個ans++就行了.

#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<cstring>
#include<cstdio>

using namespace std;

const int maxn = 100010;
int n,a[maxn],deep[maxn],head[maxn],to[maxn * 2],nextt[maxn * 2],tot = 1,ans[maxn],anss;

void add(int x,int y)
{
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;
}

void dfs(int u,int fa)
{
    deep[u] = deep[fa] + 1;
    ans[deep[u]]++;
    for (int i = head[u];i;i = nextt[i])
    {
        int v = to[i];
        if (v == fa)
            continue;
        dfs(v,u);
    }
}

int main()
{
    scanf("%d",&n);
    for (int i = 2; i <= n; i++)
    {
        scanf("%d",&a[i]);
        add(a[i],i);
    }
    dfs(1,0);
    for (int i = 1; i <= n; i++)
        if (ans[i] & 1)
            anss++;
    printf("%d\n",anss);

    return 0;
}
相關文章
相關標籤/搜索