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 ...
随机推荐
- [置顶]
【机器学习PAI实践四】如何实现金融风控
(本文数据为虚构,仅供实验) 一.背景 本文将针对阿里云平台上图算法模块来进行实验.图算法一般被用来解决关系网状的业务场景.与常规的结构化数据不同,图算法需要把数据整理成首尾相连的关系图谱.图算法更多 ...
- oracle重建undo表空间
create undo tablespace UNDOTBS2 datafile 'D:\oracle\product\10.2.0\oradata\ttonline\UNDOTBS02.DBF' s ...
- 浅谈java使用指定字符集编码,以及常见的字符集
问题的引入:在InputStreamReader(OutputStreamWriter)的构造方法中,有指定字符集编码,那么什么是字符集?有哪些常用的字符集?怎么用字符集进行编码? 一 什么是字符 ...
- ORM 模型层
一个模型就是一个单独的,确定的数据的信息源,包含了数据的字段和操作方法.通常,每个模型映射为一张数据库中的表 基本原则: 每个模型在Django中的存在形式为一个python类 每个模型都是djang ...
- 【MFC】MFC绘图不闪烁——双缓冲技术
MFC绘图不闪烁——双缓冲技术[转] 2010-04-30 09:33:33| 分类: VC|举报|字号 订阅 [转自:http://blog.163.com/yuanlong_zheng@126/ ...
- description方法
1.NSLog回顾 众所周知,我们可以用NSLog函数来输出字符串和一些基本数据类 1 int age = 11; 2 NSLog(@"age is %i", age); * 第2 ...
- numpy 矩阵相关函数
我们 知道,矩阵在python里面用的不少,所以记载下关于矩阵的操作 numpy.zeros():可以用来构造全零矩阵 >>> zeros(3) array([ 0., 0., ...
- LeetCode IPO
原题链接在这里:https://leetcode.com/problems/ipo/description/ 题目: Suppose LeetCode will start its IPO soon. ...
- Spring IOC容器的初始化-(二)BeanDefinition的载入和解析
前言 1.在讲BeanDefinition的载入和解析之前,我们先来看看什么是BeanDefinition. Bean对象在Spring中是以BeanDefinition来描述的,也就是说在Sprin ...
- django中的@login_required
转:http://www.cnblogs.com/ccorz/p/Django-zhong-loginrequired-yong-fa-jian-jie.html 1.网站开发时的登录需求: ===用 ...