【CodeForces - 931D】【 Peculiar apple-tree 】

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/82048737

题目:

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

扫描二维码关注公众号,回复: 3009337 查看本文章
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 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.

题意:就是给我们一棵树的构造,然后每个节点都会在开始结果,果子会顺着枝干的路径向底部滑去,然后每两个果子在一个节点相遇就会撞碎,问最后在底部能够接到多少果子。

解题思路:刚上来确实是入坑了,一直深陷dp怎么搞,死循环中,最后训练结束后,参考题解后发现没有那麽负责,因为每层最终能给底部带来的果实数量 只有0 或1 个。因为在中间过程中都会碰撞,所以咱们只要记录每层有多少果子,最后每层对2取余加和即可。确实直接想到很难,都深陷用各种算法解决。。= =

ac代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<stack>
using namespace std;
int nextt[100005];
int num[100005];
int level[100005];
int anser = 0;
int main()
{
        int n;
        while(scanf("%d",&n)!=EOF)
        {
        	memset(dp,0,sizeof(dp));
        	memset(level,0,sizeof(level));
        	anser=0;
	    	level[1] = dp[1] = 1;
	        for (int i = 2; i <= n; i++)
	        {
	                scanf("%d", &nextt[i]);//存放它下一层节点的位置 ,存放关系 
	                num[i] = num[nextt[i]] + 1;//dp用来存放由下层向上层递推得到这层的果子的数目 
	                level[num[i]]++;//累计这层实际能有多少到达底部的数目 
	        }
	        for(int i=1;i<=100000;i++)
	        {
	                if(level[i]&1)//判断奇偶 ,奇数加一偶数归零 
	                anser++;
	        }
	        cout<<anser<<endl;
		}
       
        return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/82048737
今日推荐