CodeForces - 931D 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.

Example
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朵花,假设每朵花都能接一个苹果,假设都同时熟,先输入一个n,为这个树上有n个苹果,从1~n,下一行有n-1个数,为第2~n个结点上的苹果能经过一秒之后掉落的节点,只有苹果掉落到1节点上才能收;掉落的过程满足:

在掉落过程中,1、若在一个节点上同时存在偶数个苹果,则全部消失,若存在奇数个苹果,则剩下一个;2、只能掉落到规定的结点上;

思路:因为规定只有掉落到树根1号结点才能收,所以找每个层上的苹果有多少个,因为两两会抵消,所以这一层上的苹果数偶数,则这一层上的苹果,在掉落过程中一定会抵消完,若为奇数,最后落到1号结点一定就剩一个,还有就是,这一层的苹果在掉落过程中,一定不会遇到其他层的苹果,因为都在同时掉落;  这道题就是找每一层上的苹果数就行了; 一定要要从树根开始找,因为树根为一个节点,数尖为多个结点; 还有规定树根为第一层,谁能掉落到树根上的结点为第二层,以此类推;若从数尖没法规定第几层; 还有从树根搜的话,根本不用标记树的结点有没有走过(想想树的结构);从树根搜的话,要存每一个结点前驱;

代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
#define Max 100010
vector<int>v[Max];
int num[Max];
int	Ma,n;
void init()
{
	int i;
	for(i = 0; i <= n;i ++)
	{
		v[i].clear();
		num[i] = 0;
	}	
}

void dfs(int f,int step)  // 	f为树的每个结点的前驱,step第几层; 
{
	num[step] ++;
	Ma = min(step,Ma);
	for (int i = 0;i < v[f].size();i++)
		dfs(v[f][i],step + 1);
}
int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		init();
		int k;
		for(i=2;i<=n;i++)
		{
			scanf("%d",&k);
			v[k].push_back(i);  //存每一个结点前驱; 
		}
		Ma = -1;
		dfs(1,1);
		int sum = 0;
		for(i = 1;i <= n;i++)
		{
			sum += num[i]%2;
		}
		printf("%d\n",sum);
	}
	return 0;
}

GD32F4系列定时器正交译码器实战:用TIMER1读取旋转编码器信号(附完整代码) 本文详细介绍了GD32F4系列定时器正交译码器的实战应用,重点讲解如何使用TIMER1模块高效读取旋转编码器信号。内容涵盖硬件连接、定时器配置、方向判断与速度计算等关键环节,并提供了完整的代码实现和优化建议,帮助开发者快速掌握GD32微控制器在工业控制和消费电子领域的编码器接口设计。 阅读详情

相关推荐

Stable-Baselines3性能优化:多环境训练对比指南(DummyVecEnv vs SubprocVecEnv)

本文深入对比了Stable-Baselines3中DummyVecEnv与SubprocVecEnv两种向量化环境的性能差异与应用场景。DummyVecEnv适用于轻量级、步进快的环境,调试友好且开销低;而SubprocVecEnv则通过真正的多进程并行,能显著加速计算密集型环境的训练。文章通过基准测试与决策树,指导开发者根据环境计算成本、CPU核心数等关键因素做出最优选择,以实现训练效率最大化。

weixin_29063979的博客 152

Codeforces 931D 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 n...

包子不卖包子 720

在Jetson orin板卡上部署ocr模型遇到错误

摘要:安装ONNX Runtime GPU版本时遇到问题,直接pip install onnxruntime-gpu会报错。解决方案是从test.pypi.org下载对应的.whl文件(如ort_nightly-1.14.0...aarch64.whl),本地安装后还需降级NumPy版本(如pip install numpy==1.26),以解决NumPy版本兼容性问题。整个过程涉及三个关键步骤:下载正确架构的wheel文件、本地安装、调整NumPy版本。

xdxd3c的博客 1529

#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 areninflorescences, numbered from1ton. Inflorescence n...

red_red_red的博客 355

个人信息保护合规审计人员(中级)能力测试试卷

本文是关于个人信息保护合规考试的试题解析,内容涵盖《个人信息保护法》相关规定。试卷包含单选题、多选题、判断题、简答题和案例分析题五种题型,总分100分,考试时间120分钟。试题重点考察个人信息处理的基本原则、敏感个人信息处理要求、合规审计流程、个人信息保护影响评估等内容。通过案例分析题详细解析了人脸识别登录、数据共享、跨境传输等场景下的合规要求。参考答案均严格依据《个人信息保护法》等法律法规,为个人信息处理活动提供了明确的合规指引。

qq_41525079的博客 900

钣金设计高薪进阶:从材料工艺到DFMA降本增效全解析

钣金设计是机械制造领域的核心技术之一,它涉及将金属板材通过切割、折弯、焊接等工艺成型为所需零件。其核心原理在于深刻理解材料特性、制造工艺约束与结构力学,以确保设计在功能、成本与可制造性之间取得最佳平衡。这项技术的价值在于,优秀的钣金设计能直接驱动产品降本增效,通过优化材料利用率、简化装配工序和避免设计缺陷,为企业节省大量模具与生产成本。其应用场景广泛,从机箱机柜、汽车部件到电子设备外壳无处不在。本文将聚焦于实现这一价值的关键路径,深入探讨面向制造与装配的设计(DFMA)原则在钣金领域的实战应用,并剖析如何通

weixin_34301132的博客 401

CF 1056D Decorate Apple Tree

题目大意: 有一颗树,可以往节点上挂灯泡(有很多种颜色),定义一个happy节点:这颗节点子树的所有叶子节点所挂的灯泡颜色不同 现在问你有1到n个happy节点,分别需要最小多少种颜色来装饰灯泡 1.一个结点可以代表一颗子树,n个结点可以代表n个子树 2.一个happy结点的cost为该子树的叶子结点数 不难得到,k个happy结点,即用前k小的cost的结点 可以dfs,得到所有结点的叶子结点,然后排序,从小到大输出即可 const int N = 1e6+10; vector<int> .

GrittyB的博客 220

CPU核心、die和package的区别:从硬件小白到高手的进阶指南

本文深入浅出地解析了CPU核心、Die与Package的核心概念与区别,是硬件爱好者从入门到精通的实用指南。文章从硅晶圆制造讲起,阐述了物理核心与逻辑线程(如超线程技术)的工作原理,并详细介绍了封装(Package)的作用与多Die模块化设计。最后,结合实际应用场景,提供了选购、性能解读与优化调优的实用建议。

pz89012345的博客 207

CodeForces - 931D Peculiar apple-tree(深搜)

Peculiar apple-treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn Arcady's garden there grows a peculiar apple-tree that fruits one time per ye...

LP_Cong 1342

audio框架学习-Audio_Flinger分析

文章目录一、代码位置二、audio服务启动2.1 main服务2.2 audioflinger添加到系统服务中2.3 audioflinger的对外接口 之前记录过audioTrack的笔记,这次继续参考audioFlinger相关的代码并做笔记; 参考文档:https://www.cnblogs.com/innost/archive/2011/01/15/1936425.html和https://blog.csdn.net/zyuanyun/article/details/60890534?spm=100

永不秃头的程序员的博客 2139

LLC变压器,常见的4大问题解决方案

其中之一是当付边的绕组的匝数或层数较多的时候,层间或匝间寄生电容和付边的漏感发生一个寄生的振荡,轻载的时候,这个振铃的幅度会达到很高,导致输出电压比设计的要高许多。但当输入是低压的时候,开关频率比较低,LLC谐振回路的增益较大,因而比较容易发生变压器饱和的问题。由于LLC变压器工作在LC谐振状态,LC谐振回路有个特点就是Q值问题,在这里Q值是大于1的,因而就会有实际加在变压器上的电压要比输入电压高的问题,因而在设计变压器的时候就必须考虑到这一点,否则变压器就不是工作在你设计的磁感应强度上。

1279

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

海绵的博客 475

【学习系列】SAP RAP 17:RAP应用部署集成至Fiori Launchpad 【S4HC Public Edition】

本文详细介绍了在SAP S/4HANA Cloud环境中部署RAP应用集成到Fiori Launchpad的两种方法:使用BAS(SAP Business Application Studio)和VSCode。主要步骤包括:1)在BTP中配置Destination连接ES环境;2)设置信任关系;3)创建BAS开发空间;4)通过Fiori应用生成器部署RAP应用;5)创建IAM App、Business Catalog和Business Role;6)配置Launchpad Space和Pages。文章还提供

DeveloperMrMeng的博客 974

#468 div2 D. Peculiar apple-tree

                                                                    D. Peculiar apple-tree                                                                        time limit per test           ...

ac_blood的博客 566

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

CodeForces 931 D. Peculiar apple-tree

题意:    有n个苹果长在树上,【i】位置的苹果下一秒会掉到p【i】位置。如果有两个苹果掉到一个位置两个苹果就都碰坏了,3个的话就有一个好的。每次只能接到1位置的苹果。分析:    通过分析我们其实可以发现只要考虑到1位置的步数就可以了。#include&lt;bits/stdc++.h&gt; using namespace std; #define N 100005 int n,res=0,...

努力打工 462

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

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

Welcome to csdn_PatrickStar's blog 365

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Codeforces Round #468 D. Peculiar apple-tree[图DFS+思维]

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 pecu

Haipai1998的博客 523

codeforces 931 A. Friends Meeting

移动的花费逐渐增加时的贪心算法

toby的博客 475

Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round) D. Peculiar apple-tree

D. Peculiar apple-treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn Arcady's garden there grows a peculiar apple-tree that fruits one time per...

看起来喜欢做枯燥事情的人或许只是更清楚自己想要什么罢了 393

Apple Tree(树状数组+线段树)

Apple TreeDescription: There is an apple tree outside of kaka’s house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple

cax1165 2863

CodeForces 349D Apple Tree(树形DP)

题目链接:codeforces.com/problemset/problem/349/D 题意:给一棵树,树的每个叶子节点上有权值,定义一颗树平衡:对于每一个结点u的子树都拥有相同的权值之和,问至少要减掉多少权值才能使树平衡 思路:对于每一个结点u,要知道它的总分支数r[u]及现在所拥有的权值和val[u],因为不同子树总分支数不一定相同,故结点u每次减少的值需要是其所有子树分支的...

Tongqi 1104

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 areninflorescences, numbered from1ton. Inflorescence...

weixin_30493321的博客 144

【CodeForces - 931D】【 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. Infloresce...

哇哈哈哈的博客 299

codeforces 1056 D. Decorate Apple Tree (简单dfs)

D. Decorate Apple Tree time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output There is one apple tree in Arkady's garden. It can be repre...

Amo丶 506

【Mail.Ru Cup 2018 Round 3 D. Decorate Apple Tree】排序+思维

D. Decorate Apple Tree 题意 给你一棵树每个叶子节点上都有一个颜色, 如果某个节点是好节点,要满足他子树内所有的叶子节点颜色不同 对每个k∈[1,n]对每个k\in[1,n]对每个k∈[1,n]输出至少有k个点为好点最少需要多少种颜色 做法 每次选出的k个点最后只有一个点是有效的,也就是子树叶子节点最多的那个点 选n个点时一定是根,选n-1个点时一定是去掉根之后包含叶子节点...

lajiyuan的博客 1090

【CF#468 div2 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 areninflorescences, numbered from1ton. Infloresce...

xuanweiace的博客 520
上一篇: CodeForces - 931C (思维题,最少相等数)
下一篇: L2-001. 紧急救援(distar 求最短路的变形)
obsorb_knowledge
博客等级 码龄9年 38粉丝 213原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值