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

题意:一棵树上1~N个节点,1为基部节点,给定从2~N的节点的每个节点连接的下一个节点。现在每个节点产生一个苹果,每过一秒向下滚动一个节点,如果有偶数个的苹果同时汇入同一节点则碰撞消失,问总共能收集多少苹果。
因为最后的汇点唯一,所以同一层的苹果必然会在下面的某一个节点相遇,那么只需要从下向上做一次bfs,再统计每一层苹果数量,偶数则答案加一。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define pn printf("\n")
using namespace std;
typedef long long ll; const int maxn = 1e5+;
int dep = ;
struct edge{
int to, next;
}e[maxn], _e[maxn];
int head[maxn], tot;
int n, cur[maxn], cnt[maxn]; void init()
{
tot = ;
memset(head, -, sizeof head);
} void addedge(int u,int v)
{
e[tot].to = v;
e[tot].next = head[u];
head[u] = tot ++;
} void bfs()
{
queue <int> que;
cur[] = ;
que.push(); while(!que.empty())
{
int a = que.front();
que.pop();
dep = max(dep, cur[a]);
cnt[cur[a]]++; for(int i=head[a]; i!=-; i=e[i].next)
{
int v = e[i].to;
cur[v] = cur[a] + ;
que.push(v);
}
}
} int solve()
{
int res = ;
for(int i=;i<=dep;i++)
if(cnt[i]&)
res ++;
return res;
} int main()
{
init();
scanf("%d",&n);
int x;
for(int i=;i<n-;i++)
{
scanf("%d",&x);
addedge(x, i+);
}
bfs(); printf("%d\n", solve());
}

Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)D. Peculiar apple-tree的更多相关文章

  1. Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)B. World Cup

    The last stage of Football World Cup is played using the play-off system. There are n teams left in ...

  2. Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)

    A.B都是暴力搞一搞. A: #include<bits/stdc++.h> #define fi first #define se second #define mk make_pair ...

  3. Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)C. Laboratory Work

    Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some v ...

  4. Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)A. Friends Meeting

    Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the ...

  5. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】

    C. Did you mean... time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  6. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】

    B. Which floor? time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  7. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】

    A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  8. Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

    A. Technogoblet of Fire 题意:n个人分别属于m个不同的学校 每个学校的最强者能够选中 黑客要使 k个他选中的可以稳被选 所以就为这k个人伪造学校 问最小需要伪造多少个 思路:记 ...

  9. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

    A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...

随机推荐

  1. ASP的Global.asa使用说明

    /*-------------------ASP文档参考集-----------------------*/ *-->作者:草履虫 *-->时间:2007-4.28---2007-4.30 ...

  2. 关于约束ENABLE NOVALIDATE的一个疑问

    http://www.dbunix.com/?p=188 关于约束ENABLE NOVALIDATE的一个疑问 CREATE TABLE test (id varchar2(12), name var ...

  3. SSL延迟

    原文链接 据说,Netscape公司当年设计SSL协议的时候,有人提过,将互联网所有链接都变成HTTPs开头的加密链接. 这个建议没有得到采纳,原因之一是HTTPs链接比不加密的HTTP链接慢很多.( ...

  4. 基于java注解实现自己的orm框架

    ORM即Object Relation Mapping,Object就是对象,Relation就是关系数据库,Mapping映射,就是说Java中的对象和关系数据库中的表存在一种对应关系. 现在常见的 ...

  5. LINUX内核内存屏障

    =================                          LINUX内核内存屏障                          ================= By ...

  6. likely, unlikely的作用

    在项目中看到了likely.unlikely宏的使用, 一直不是非常清楚它们的作用,所以就深究下. likely表示被測试的表达式大多数情况下为true, unlikely则表示相反. 两个宏定义: ...

  7. 【solr基础教程之中的一个】Solr相关知识点串讲

           Solr是Apache Lucene的一个子项目.Lucene为全文搜索功能提供了完备的API.但它仅仅作为一个API库存在.而不能直接用于搜索. 因此,Solr基于Lucene构建了一 ...

  8. 学习日记之原型模式和Effective C++

    原型模式(Prototype):用原型实例制定创建对象的种类,而且听过拷贝这些原型创建新的对象. 浅复制:假设字段是值类型的,则对该字段运行逐位复制.假设字段是引用类型.则复制引用但不复制引用的对象. ...

  9. EOJ 2822 内存显示

    一个 int 类型变量或 double 类型变量在连续几个字节的内存中存放.读取数值时,当数值中包含小数点时类型为 double,否则类型为 int.将读入的数值存放在 int 类型变量或 doubl ...

  10. Quartz实例:quartz定时任务代码示例

    转自:http://www.blogchong.com/post/96.html quartz定时任务调度框架,使用实例. Job类://即实际调度任务实现 . package net.csdn.ed ...