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 ...
随机推荐
- Django自带后台使用配置
参考官网地址:https://docs.djangoproject.com/en/1.11/ref/contrib/admin/ ,本文章值是介绍简单配置,如果需要详细内容可以查阅官方文档 自动管理界 ...
- Java学习笔记三:Java的变量、常量、变量的类型及使用规则
Java的变量.常量.变量的类型及使用规则 每一种语言都有一些具有特殊用途的词,Java也不例外,它们被称为关键字.关键字对 Java 的编译器有着特殊的意义. 一:Java中的关键字: 注:Java ...
- Electronic Devices【电子设备】
Electronic Devices We may think we're a culture that gets rid of our worn technology at the first si ...
- C语言实现二分查找
二分查找优势:比顺序查找更有效率 特点:元素按顺序排列 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include ...
- python2.7入门---模块(Module)
来,这次我们就看下Python 模块(Module).它是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句.模块让你能够有逻辑地组织你的 Pytho ...
- 反向代理服务器——nginx
一.概述 先来看百度百科的介绍: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.其特点是占有内存少,并发能力强 ...
- mysql字符串拼接,存储过程
添加字段: alter table `user_movement_log`Add column GatewayId int not null default 0 AFTER `Regionid` (在 ...
- ant-design 实现一个登陆窗口
前提:已经完成项目实战(https://ant.design/docs/react/practical-projects-cn#定义-Model) 如果要想实现一个登陆窗口,首先得有一个ui,想到的是 ...
- 利用LD_PRELOAD进行hook
原文地址:http://hbprotoss.github.io/posts/li-yong-ld_preloadjin-xing-hook.html 好久没玩hook这种猥琐的东西里,今天在Linux ...
- Qt 隐藏标题栏可移动升级版
在最出的时候,在Qt程序隐藏标题栏的情况下,实现界面可拖拽移动,是鼠标在在程序界面的任意位置都可以,现在这个版本是需要鼠标在程序界面的特定位置开可以 上代码 static QPoint last(0, ...