Codeforces 931D Peculiar apple-tree(dfs+思维)
题目链接:http://codeforces.com/contest/931/problem/D
题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个。两个两个会抵消苹果。问最后在根节点能收到多少个苹果。
解题思路:昨天是我想复杂了,其实就是统计下每层的苹果数,若是奇数则答案+1。因为最终这些苹果都会滚落汇聚到根节点,所以在滚落过程中肯定会碰撞并相消无论苹果是怎么分布的。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N=1e5+; int dep_num[N];
vector<int>v[N]; void dfs(int u,int dep){
dep_num[dep]++;
for(int i=;i<v[u].size();i++){
int t=v[u][i];
dfs(t,dep+);
}
} int main(){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
int fa;
scanf("%d",&fa);
v[fa].push_back(i);
}
dfs(,);
int ans=;
for(int i=;i<N;i++){
ans+=dep_num[i]%;
}
printf("%d\n",ans);
return ;
}
Codeforces 931D Peculiar apple-tree(dfs+思维)的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- 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 ...
- POJ3321 - Apple Tree DFS序 + 线段树或树状数组
Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...
- Codeforces 348B:Apple Tree(DFS+LCM+思维)
http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点 ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
- CodeForces 620E:New Year Tree(dfs序+线段树)
E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...
- codeforces 916E Jamie and Tree dfs序列化+线段树+LCA
E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- npm publish gives “unscoped packages cannot be private”
解决方法: npm publish --access public 详细参考此处
- idea中复制module和module中的蓝色tag出现的方法
1.在从github上面导入项目到idea中时,经常好多module都是没有蓝色的tag的,这说明这不是个maven形式的module,需要导入到项目中. 举个例子: 有蓝色tag的module才可以 ...
- Dockerfile 部署 nodejs
1.编写.dockerignore 构建镜像时,并不需要node_modules目录等内容,可以使用.dockerignore忽略一些文件 # .dockerignore Dockerfile nod ...
- sql service ---- update和delete 误操作数据 ---- 恢复数据
原文出处:http://blog.csdn.net/dba_huangzj/article/details/8491327 问题: 经常看到有人误删数据,或者误操作,特别是update和delete的 ...
- 科学计算三维可视化---Traits属性的功能
Traits属性的功能 Traits库为python的属性增加了类型定义的功能,除此之外他还提供了5个特殊的功能(可以为用户的界面响应提供基本能力):初始化:每个traits属性都有自己的默认值验证: ...
- Lena与数字图像处理
在数字图像处理中,Lena(Lenna)是一张被广泛使用的标准图片,特别在图像压缩的算法研究中. 黑白Lena图 标准Lena (为什么用这幅图,是因为这图的各个频段的能量都很丰富:即有低频(光滑 ...
- ip地址、子网掩码、DNS的关系与区别
首先ip地址可能表示内网或者外网地址: 内网也就是局域网,最直观的就是像网吧,公司内部的电脑用交换机,HUB,路由连起来的.再通过光钎.猫接入INTERNET的. 外网就像你家里的一台电脑.用猫拨号上 ...
- JavaApplet-Application Blocked..Your security setting have blocked an untrusted application from running..
From:http://www.cnblogs.com/killerlegend/p/3892668.html Author:KillerLegend Date:2014.8.5 在看A*算法的时候, ...
- vscode nodejs智能提示功能
1.依赖一些第三方的插件,先安装typings这个包,如果使用的是淘宝镜像,输入cnpm.cmd执行: cnpm i typings -g //cnpm install typings -global ...
- 七、新时间日期 API
一.使用 LocalDate. LocalTime. LocalDateTime LocalDate. LocalTime. LocalDateTime 类的实例是不可变的对象,分别表示使用 ISO- ...