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

分析

这对我来说就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. Leetcode 1014. Best Sightseeing Pair

    本题是leetcode121这道题的翻版,做法完全一样,也是扫一遍数组,维护两个值,一个是a[i]+i的最大值,另一个是a[i]+a[j]+i-j的最大值. class Solution: def m ...

  2. Cisco DHCP 配置要点

    实验拓扑图:IOU5/6/7模拟主机 IOU1为DHCP服务器 IOU2为DHCP中继器 IOU3/4为局域网内的交换机 在IOU1中配置DHCP配置 IOU2作为DHCP中继,在E0/0.10和E0 ...

  3. [转载] C++中this指针的用法详解

    摘自:http://blog.chinaunix.net/uid-21411227-id-1826942.html 1. this指针的用处: 一个对象的this指针并不是对象本身的一部分,不会影响s ...

  4. 使用iptables nat进行端口转发

    1.将发向HostA:PortA的请求转发到HostB:PortB iptables -t nat -A PREROUTING -p tcp -i eth0 -d HostA --dport Port ...

  5. [转载] ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

    这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...

  6. PHP读取文件函数fread,fgets,fgetc,file_get_contents和file函数的使用总结

    fread().fgets().fgetc().file_get_contents() 与 file() 函数用于从文件中读取内容. 1.fread() fread()函数用于读取文件(可安全用于二进 ...

  7. C++常量const

    常量折叠概念 常量折叠表面上的效果和宏替换是一样的,只是“效果上是一样的”,而两者真正的区别在于,宏是字符常量,在预编译宏替换完成后,该宏名字会消失,所有对宏的引用已经全部被替换为它所对应的值,编译器 ...

  8. numpy 矩阵相关函数

    我们 知道,矩阵在python里面用的不少,所以记载下关于矩阵的操作 numpy.zeros():可以用来构造全零矩阵 >>> zeros(3) array([ 0.,  0.,   ...

  9. 转载FPGA学习之内嵌乘法器调用

    补充一点,除法的时候如果直接a/b那么就会调用lpm模块,不管输入是否是常数,乘法的时候输入都是reg型变量会调用硬件乘法器,有一个是常数就会调用lpm模块. 上课的时候一直听老师说真正实践的时候你别 ...

  10. fn project 私有镜像发布

    1. 说明 fnproject 默认的docker registry 是 dockerhub 对于企业应用还是不太方便的 还好系统系统了配置参数方便我们进行配置,与开源harbor 进行集成 2. 使 ...