codeforce468DIV2——D. Peculiar apple-tree
题意
给你一颗树,开始时每个结点都有一个小球,每一秒钟每个小球都往上滚一层,当两个球在同一个结点的时候会被消去,如果三个五个七个等在同一个结点的化消去后只剩一个。
分析
这对我来说就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的更多相关文章
- 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 ...
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- 【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 ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- 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 ...
- POJ3321 Apple Tree (树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16180 Accepted: 4836 Descr ...
- Apple Tree(需要预处理的树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20335 Accepted: 6182 Descr ...
随机推荐
- Java虚拟机运行时数据区域划分
Java虚拟机数据运行时区域 方法区(Method Area) 存储加载的类信息,常量,静态变量,编译器编译后的代码等数据.虽然JVM规范把方法区描述为堆的一个逻辑部分,但它却有一个别名叫做N ...
- HTTP浅析
引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1. ...
- IOS socket编程--Asyncsocket
iPhone的标准推荐是CFNetwork 库编程,其封装好的开源库是 cocoa AsyncSocket库,用它来简化CFNetwork的调用,它提供了异步操作 主要特性有: 队列的非阻塞的读和写, ...
- sublime text3 中设置默认浏览器,并且设置快捷键
1.打开packageControl 对应快捷键 command + shift + p 2.输入install package 3.安装插件 SideBarEnhancements 4.安装了 ...
- goreplay(gor) golang 流量拷贝工具试用
1. 项目地址 https://github.com/buger/goreplay 2. 安装 wget https://github.com/buger/goreplay/releases/down ...
- oracle系统表的查询
oracle查询用户下的所有表 select * from all_tab_comments -- 查询所有用户的表,视图等select * from user_tab_comments -- 查 ...
- Spring AOP声明式事务异常回滚
近日测试用例,发现这样一个现象:在业务代码中,有如下两种情况,比如:throw new RuntimeException("xxxxxxxxxxxx"); 事物回滚throw ne ...
- JAVA反射调用方法
1.用户类 package com.lf.entity; import com.lf.annotation.SetProperty; import com.lf.annotation.SetTable ...
- php跨域问题
http://www.cnblogs.com/xiezn/p/5651093.html
- Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
python3.6.3 我在处理爬虫时候使用BeautifulSoup中遇到报错 “ bs4.FeatureNotFound: Couldn't find a tree builder with t ...