Codeforces 931.D Peculiar apple-tree
1 second
256 megabytes
standard input
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.
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.
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.
3
1 1
1
5
1 2 2 2
3
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
4
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的更多相关文章
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- CodeForces 812E Sagheer and Apple Tree 树上nim
Sagheer and Apple Tree 题解: 先分析一下, 如果只看叶子层的话. 那么就相当于 经典的石子问题 nim 博弈了. 那我们看非叶子层. 看叶子层的父亲层. 我们可以发现, 如果从 ...
- Codeforces 812E Sagheer and Apple Tree
大致题意: 给你一颗树,这个树有下列特征:每个节点上有若干个苹果,且从根节点到任意叶子节点的路径长度奇偶性相同. 甲和乙玩(闲)游(得)戏(慌). 游戏过程中,甲乙轮流将任意一个节点的若干个苹果移向它 ...
- Codeforces 812E Sagheer and Apple Tree ——(阶梯博弈)
之前在bc上做过一道类似的阶梯博弈的题目,那题是移动到根,这题是移动到叶子.换汤不换药,只要和终态不同奇偶的那些位置做nim即可.因此这题给出了一个条件:所有叶子深度的奇偶性相同.同时需要注意的是,上 ...
- Codeforces 348B - Apple Tree
348B - Apple Tree 我们设最后答案为 x , 我们我们就能用x表示出所有节点下面的苹果个数, 然后用叶子节点求lcm, 取最大的可行解. #include<bits/stdc++ ...
- cf202-div 1-B - Apple Tree:搜索,数论,树的遍历
http://codeforces.com/contest/348/problem/B B. Apple Tree time limit per test 2 seconds memory l ...
- POJ 2486 Apple Tree
好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
随机推荐
- python基础,导入模块,if语句,while语句
python基础 python代码 变为字节码 变为机器码 最后执行执行‘文件名.py’文件时出现的‘文件名.pyc’文件为字节码 缓存机制 使用pycharm的时候在文件最开始添加下面这两行代码,中 ...
- [Cracking the Coding Interview] 4.4 Check Balanced
Implement a function to check if a binary tree is balanced. For the purpose of this question, a bala ...
- Andrid 打印调用堆栈
public static void printCallStatck() { Throwable ex = new Throwable(); StackTraceElement[] stackElem ...
- Hibernate-ORM:08.Hibernate中的投影查询
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将叙述hibernate中的投影查询 一,目录: 1.解释什么是投影查询 2.返回Object单个对象 ...
- 【数据结构】 List 简单实现
public class XList<T> : IEnumerable, IEnumerator { #region List 简单实现 /// <summary> /// 存 ...
- android activity状态保存
一.被其他任务打断(来电话),再次打开希望保留数据 private String SAVE_INSTANCE_TAG = "ATWAL"; @Override protected ...
- windows10安装liux系统
1.前言 因为大部分服务器都是linux系统,需要掌握linux命令行和熟悉linux环境,所以自己用为数不多的工资买了新电脑,就是为了学习linux系统,此文是为了记载自己在windows系统上安装 ...
- hdu1505City Game(动态规划)
City Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- Python-学习-项目1-即时标记-1
买了一本Python入门,奈何看不下去,只能是先看后面的项目,看到那里不懂的时候在回去学习. 项目名字:即时标记 大致的意思就是把一个纯文本文件标记成自己想要的格式文件. 首先就是待处理文本,我找不到 ...
- 走进Android系统
一.Android背景 [Android定义] Android是Google公司在2007年11月5日公布的基于Linux平台的开源手机操作系统. [发展历程] 2005年,Google收购企业And ...