Codeforces 931D 【DFS】

一棵特殊的苹果树在结果时,每个花序结一个苹果,并沿树枝滚到树底。两个苹果相遇会消失,只有一个会存活。帮助Arcady计算他能在一次收获中从第一花序收集到多少个苹果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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.

Solution

有一棵特殊的苹果树,从根到顶部的序号分别是1~n,给出每个序号会掉落到的位置,每一秒进行一次掉落,如果该位置有偶数个苹果就会消除,奇数个苹果就会剩下一个,然后继续往下掉,掉到序号1可以被收集到,初始序号1有1个苹果。问整棵树所有苹果掉完能收集多少个苹果。从序号1开始往后找,在第 i 秒的时候有多少个位置的苹果会出现掉落情况,因为最终都会到序号1,所以只需要把该秒掉落的苹果数量判断奇偶即可。

Code

#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+5;;

int n,cnt[maxn],maxxtime;
vector<int> v[maxn];

void dfs(int now,int secondd) {
	cnt[secondd]++;
	for(int i=0; i<v[now].size(); i++) {
		dfs(v[now][i],secondd+1);
	}
	maxxtime = max(maxxtime,secondd);
}

int main() {
	while(cin >> n) {
		int temp;
		for(int i=0; i<=n; i++) {
			v[i].clear();
		}
		memset(cnt, 0, sizeof(cnt));
		for(int i=2; i<=n; i++) {
			cin >> temp;
			v[temp].push_back(i);
		}
		dfs(1,0);
		int ans = 0;
		for(int i=0; i<=maxxtime; i++) {
//			cout << i << " " << cnt[i] << endl;
			ans += (cnt[i] & 1);
		}
		cout << ans << endl;
	}
}

Source

Codeforces 931D Peculiar apple-tree

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值