#CF——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

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-1 个数字,是该苹果对应的父节点的层次位置

思路:

这里因为是每个层次的苹果都是同时掉落,所以不同层次的苹果是不可能碰到一起的,所以只需要判断每个层次苹果的奇偶就可以了

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
const int maxn=1e6+5;
typedef long long ll;
using namespace std;
int vis[maxn];
int depth[maxn];
int Max=-1;
int main(){
    int n,tmp;
	scanf("%d",&n);
	depth[1]=1;
	for(int i=2;i<=n;i++){
		scanf("%d",&tmp);
		depth[i]=depth[tmp]+1;
		Max=max(Max,depth[tmp]);
		vis[depth[tmp]]++;
	} 
	int sum=0;
	for(int i=1;i<=Max;i++){
		if(vis[i]%2)
		    sum++;
	}
	printf("%d\n",sum+1);
    return 0;
}

网上还有dfs的版本,其实也就是处理每一层上有多少苹果

#include<iostream>
#include<vector>
using namespace std;
int vis[1000005];
int depth[1000005];
vector<int> v[1000005];
void dfs(int t,int deep){
	depth[t]=deep;
	for(int i=0;i<(int)v[t].size();i++){
		dfs(v[t][i],deep+1);
	}
}
int main(){
	int n;
	scanf("%d",&n);
	depth[1]=1;
	for(int i=2;i<=n;i++){
		int tmp;
		scanf("%d",&tmp);
		v[tmp].push_back(i); 
	}
	dfs(1,0);
	for(int i=1;i<=n;i++)
	    vis[depth[i]]++;
	int sum=0;
	for(int i=1;i<=n;i++){
		if(vis[i]%2)
		    sum++;
	}
	printf("%d\n",sum+1);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L). To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river. Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N). FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks. Input Line 1: Three space-separated integers: L, N, and M Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position. Output Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks Sample Inputcopy Outputcopy 25 5 2 2 14 11 21 17 4 Hint Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值