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. poj 3006水题打素数表

    #include<stdio.h> #include<string.h> #define N 1100000 int isprim[N],prime[N]; void ispr ...

  2. 洛谷 P2412 查单词

    P2412 查单词 题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个 ...

  3. How to run Java main class and pass application arguments in Maven?

    原文: http://www.logicbig.com/how-to/maven/mvn-java-exec-args/ --------------------------------------- ...

  4. Git .gitignore文件忽略

    Git .gitignore文件忽略 学习了:http://blog.csdn.net/yonnangel/article/details/50115059 http://www.cnblogs.co ...

  5. 从编程的角度理解gradle脚本﹘﹘Android Studio脚本构建和编程[魅族Degao]

    本篇文章由嵌入式企鹅圈原创团队.魅族资深project师degao撰写. 随着Android 开发环境从Eclipse转向Android Studio,我们每一个人都開始或多或少要接触gradle脚本 ...

  6. [ACM] hdu 1035 Robot Motion (模拟或DFS)

    Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...

  7. LINQ查询知识总结

    -------适合自己的才是最好的!!! LINQ查询知识总结:案例分析 案例:汽车表car,系列表brand,厂商表productor private MyCarDataContext  _Cont ...

  8. pip3使用

    安装好python3.5,在D:\study\python\python35\Scripts目录下打开命令行 执行命令pip3 list 查看已经安装的东西,提示需要升级,执行命令python -m ...

  9. [iOS]UITableViewController完毕收回键盘操作

    UITableViewController 本身可以实现键盘适配(cell中控件焦点会移动到键盘上方 在做键盘收回的时候思考过例如以下方案 1.tableview加入点击事件 结果:点击事件和tabl ...

  10. h5-7 canvas

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...