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 \(p_i\)-th inflorescence and \(p_i< 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 \(p_a\)-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 \(p_2, p_3, ..., p_n (1 ≤ p_i < i)\), where \(p_i\) 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.

题意

给你一棵树,有\(n\)个节点,每个节点有一个苹果,在苹果成熟后,苹果会落下来,如果有两个苹果落到了同一个节点,那么这两个苹果会消失,问最后会有多少个苹果落到根节点

思路

读题读了一年,发现是个傻逼题,嘤嘤嘤~

根据输入建好图,dfs求出每个节点的深度,然后算出各个深度有多少节点,如果是偶数个,那么最后落到根节点的苹果数一定是\(0\),反之为\(1\)

代码

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int a[maxn];
vector<int>edge[maxn];
int hei[maxn];
int num[maxn];
// 处理每个点的深度
void dfs(int father,int son)
{
hei[son]=hei[father]+1;
for(auto v:edge[son])
{
if(v!=son)
dfs(son,v);
}
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("/home/wzy/in.txt", "r", stdin);
freopen("/home/wzy/out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
for(int i=2;i<=n;i++)
{
cin>>a[i];
edge[a[i]].push_back(i);
}
dfs(0,1);
int ans=0;
map<int,int>mp;
for(int i=1;i<=n;i++)
mp[hei[i]]++;
for(auto it:mp)
ans+=(it.second&1);
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}

Codeforces 931D: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 342E :Xenia and Tree

    Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes i ...

  4. Codeforces 812E Sagheer and Apple Tree

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

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

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

  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 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...

  8. Codeforces 348B - Apple Tree

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

  9. POJ 3321:Apple Tree(dfs序+树状数组)

    题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1. 分析: 对树进行dfs,将树变为序列,记录每个 ...

随机推荐

  1. javaIO——输入输出流

    字节流与字符流 File类不支持对文件内容进行相关的操作,所有若要处理文件的内容,则需要通过流操作模式来完成. 流的基本操作步骤: Step1:根据文件路径创建File类对象. Step2:根据字节流 ...

  2. git删除了本地文件,从远程仓库中恢复

    在本地删除了文件,使用git pull,无法从远程项目中拉取下来 具体操作 查看项目的状态,会显示出你删除的数据 git status 进入被删除的文件的目录下,假设删除的文件名为 test.txt ...

  3. Linux安装软件出错

    1.Delta RPMs disabled because /usr/bin/applydeltarpm not installed. yum provides '*/applydeltarpm' # ...

  4. 【Linux】【Services】【SaaS】Docker+kubernetes(5. 安装和配置ETCD集群)

    1. 简介: 1.1. ETCD是kubernetes和openstack都用到的组件,需要首先装好 1.2. 官方网站:https://coreos.com/etcd/ 1.3. ETCD的作用: ...

  5. 对于HTML和XML的理解

    1.什么是HTML??? HTML就是 超文本标记语言(超文本含义:超过文本 --图片 .视频.音频. 超链接) 2.HTML作用 把网页的信息格式化的展现,对网页信息进行规范化展示 连接(https ...

  6. 智龙开发板搭建llsp环境

    智龙开发板搭建llsp(linux+lighttpd+sqlite3+php)环境 1. 准备 1. 智龙开发板V3 2. 软件编译环境:VirtualBox6+CentOS6.10-i386.min ...

  7. Git忽略提交规则 .gitignore文件

    在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.简单来说一个场景:在你使用git add .的时候,遇到 ...

  8. Jenkins视图管理

    目录 一.简介 二.视图维护 创建视图 将项目加入视图中 三.状态图标变绿 四.看板 一.简介 在现在的编程中,公司往往采用的是模块化的编程方式,也就是说将一个项目拆分成许多模块,每个小项目组往往只负 ...

  9. shell脚本 awk实现实时监控网卡流量

    一.简介 通过第3方工具获得网卡流量,这个大家一定很清楚.其实通过脚本一样可以实现效果.下面是我个人工作中整理的数据.以下是shell脚本统计网卡流量. 现原理: cat /proc/net/dev ...

  10. Django记录操作日志、LogEntry的使用

    LogEntry是在后台开发中经常用到的模块,它在admin是默认开启的. 可以使用LogEntry模块记录所有用户的操作记录.一方面可以用来监督,另一方面可以用来做回滚. 1. 使用LogEntry ...