题意
给你一颗树,开始时每个结点都有一个小球,每一秒钟每个小球都往上滚一层,当两个球在同一个结点的时候会被消去,如果三个五个七个等在同一个结点的化消去后只剩一个。

分析

这对我来说就TM是英语阅读理解哇!赛场上读题读到懵逼啊!

最容易想到的是一个O(N^2)的算法,最深由maxd层,从第一层枚举到maxd层然后进行DFS,来计算每一层的小球最后落下来还剩几个,但是这样肯定会被卡掉。然后就再想一个类似贪心的方法,直接dfs计算每一层的小球数量,如果偶数则全部会被消掉,如果是奇数则会剩一个。这样做为什么可以,因为可以忽略中间的过程,任何一层的小球最后都会到根。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector> using namespace std;
const int maxn=+;
vector<int>G[maxn];
int n;
int maxd=;
int num[maxn];
void dfs(int u,int dep){
num[dep]++;
maxd=max(maxd,dep);
if(G[u].size()==)return ;
for(int i=;i<G[u].size();i++){
int v=G[u][i];
dfs(v,dep+);
}
return ;
} int main(){
scanf("%d",&n);
int a;
for(int i=;i<=n;i++){
scanf("%d",&a);
G[a].push_back(i);
}
dfs(,);
int ans=;
for(int i=;i<=n;i++){
if(num[i]%)
ans++;
}
printf("%d ",ans);
return ;
}

codeforce468DIV2——D. Peculiar apple-tree的更多相关文章

  1. POJ 2486 Apple Tree

    好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...

  2. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  3. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  4. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  5. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  6. 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...

  7. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  8. URAL 1018 Binary Apple Tree(树DP)

    Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...

  9. POJ3321 Apple Tree (树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16180   Accepted: 4836 Descr ...

  10. Apple Tree(需要预处理的树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20335   Accepted: 6182 Descr ...

随机推荐

  1. UVALive 6163(暴力枚举)

    这道题我的做法就是枚举这四个数的所有排列所有运算所有计算顺序. 略有考验代码能力,不能漏掉情况,注意模块化的思想,一些功能写成函数调试的时候结构清晰好分析. 比赛时没有AC是对next_permuta ...

  2. 伪元素:placeholder-shown&&:focus-within

    :placeholder-shown 另外,划重点,这个伪类是仍处于实验室的方案.也就是未纳入标准,当然我们的目的是探寻有意思的 CSS . 当 input 类型标签使用了 placeholder 属 ...

  3. phpcms URL修改

    修改caches\configs\system.php中'html_root' => 'html',//生成静态文件路径改成'html_root' => '',//生成静态文件路径,然后修 ...

  4. 我所常用的git命令

    说明公司向用git来管理项目的代码,我以前只是在eclipse中使用菜单来操作git,现在,学习一下命令,这样也不用安装各种git客户端软件了.git安装在官网上下载git,安装完成之后,在命令行中输 ...

  5. HDU - 6166:Senior Pan(顶点集合最短路&二进制分组)

    Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory pro ...

  6. bzoj 3195 奇怪的道路

    Written with StackEdit. Description 小宇从历史书上了解到一个古老的文明.这个文明在各个方面高度发达,交通方面也不例外.考古学家已经知道,这个文明在全盛时期有\(n\ ...

  7. NETCore中RabbitMQ的使用

    NET中RabbitMQ的使用 https://www.cnblogs.com/xibei666/p/5931267.html 概述 MQ全称为Message Queue, 消息队列(MQ)是一种应用 ...

  8. runtime获取对象所有属性(变量)和方法

    1.包含运行时头文件 <objc/runtime.h> 2.获取某个类的成员变量或者属性: unsigned int numIvars; //成员变量个数 Ivar *vars = cla ...

  9. 老罗关于binder的链接

    Android进程间通信(IPC)机制Binder简要介绍和学习计划 : http://blog.csdn.net/luoshengyang/article/details/6618363

  10. 【Xamarin 】MonoTouch - UIImageView响应点击事件

    //圆角头像 UIImageView _avatarView = new UIImageView(new RectangleF(_blockSpace, _blockSpace, 2 * _avata ...