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.

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

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.

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个苹果往根上掉,第i个分支的苹果经过1s后会掉到第pi个分支,如果一个分支在某一个时刻有偶数个苹果,这些苹果全都会消失,否则保留下一个,当苹果掉到1号点就被视为捡到,问最多能捡到多少个苹果.

分析:这题不要想复杂了......

   把这棵树给构造出来,深度为i的苹果需要跳i次才能跳到根节点,它们必然会在根节点相遇. 那么对于每一个深度,数一下有多少苹果,如果有奇数个ans++就好了.

#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<cstring>
#include<cstdio> using namespace std; const int maxn = ;
int n,a[maxn],deep[maxn],head[maxn],to[maxn * ],nextt[maxn * ],tot = ,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] + ;
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 = ; i <= n; i++)
{
scanf("%d",&a[i]);
add(a[i],i);
}
dfs(,);
for (int i = ; i <= n; i++)
if (ans[i] & )
anss++;
printf("%d\n",anss); return ;
}

Codeforces 931.D Peculiar apple-tree的更多相关文章

  1. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  2. CodeForces 812E Sagheer and Apple Tree 树上nim

    Sagheer and Apple Tree 题解: 先分析一下, 如果只看叶子层的话. 那么就相当于 经典的石子问题 nim 博弈了. 那我们看非叶子层. 看叶子层的父亲层. 我们可以发现, 如果从 ...

  3. Codeforces 812E Sagheer and Apple Tree

    大致题意: 给你一颗树,这个树有下列特征:每个节点上有若干个苹果,且从根节点到任意叶子节点的路径长度奇偶性相同. 甲和乙玩(闲)游(得)戏(慌). 游戏过程中,甲乙轮流将任意一个节点的若干个苹果移向它 ...

  4. Codeforces 812E Sagheer and Apple Tree ——(阶梯博弈)

    之前在bc上做过一道类似的阶梯博弈的题目,那题是移动到根,这题是移动到叶子.换汤不换药,只要和终态不同奇偶的那些位置做nim即可.因此这题给出了一个条件:所有叶子深度的奇偶性相同.同时需要注意的是,上 ...

  5. Codeforces 348B - Apple Tree

    348B - Apple Tree 我们设最后答案为 x , 我们我们就能用x表示出所有节点下面的苹果个数, 然后用叶子节点求lcm, 取最大的可行解. #include<bits/stdc++ ...

  6. cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

      http://codeforces.com/contest/348/problem/B   B. Apple Tree time limit per test 2 seconds memory l ...

  7. POJ 2486 Apple Tree

    好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...

  8. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  9. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

随机推荐

  1. python学习——基本数据类型

    一.运算符 1.算术运算: 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 二.基本数据类型 1.数字 1.1 整形数字和长整形数字:在32位机器上,整数的位数为32位,取值范围为-2**31 ...

  2. (转)Updates were rejected because the tip of your current branch is behind

    刚创建的github版本库,在push代码时出错: $ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] ...

  3. Vue简单使用,

    一些零碎的知识点: 在js中变量的声明 有三种方式: let,var, const let: 对应的是一个块级作用域 { let a = 12 } console.log(a) 这是未声明的, var ...

  4. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

  5. JSON初体验(二):Gson解析

    今天,我们来介绍一下Gson的jar包的用法. JSON解析之Gson 特点:编码简介,谷歌官方推荐 数据之间的转换: 1.将json格式的字符串{}转换成为java对象 API: <T> ...

  6. LeetCode:27. Remove Element(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...

  7. spring data elasticsearch多索引查询

    一次查询多个索引数据 es里可以这样写 GET 索引1,索引2,索引3/_search 也可以这样 给索引创建别名,多个索引可以使用一个别名 POST /_aliases { "action ...

  8. 02-Mysql数据库----初识

    什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中描述一个事物,就 ...

  9. python--基础篇二

    一. 格式化输出 :name=input("name:") age=input("age:") hobby=input("hobbie:") ...

  10. XML序列化器读取XML数据

    PS:标题我还真的不知道该怎么取比较好,大家将就下吧^_^ 场景:上周接到一个任务,要求我把ASP写的会员充值功能,用ASP.NET复制一遍,没有给我需求文档,就是让我根据代码去分析业务逻辑,然后看到 ...