题目:

给出一个树,这棵树上每个结点每一秒都会结出一颗果实,果实每经过一秒就会落向下一个结点,如果一个结点在同一时刻上的果实两两抵消,问最后在根节点处一共有多少个果实。

思路:

dfs直接搜索统计这棵树的每一层上有多少个果实就可以了。如果是奇数个ans++,偶数个不作处理。

代码:

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <iomanip>
#define MAX 1000000000
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin) using namespace std;
typedef long long ll;
const int maxn = ;
int n,x,ans=;
int deep[maxn];
vector<int> mp[maxn]; void init()
{
memset(deep,,sizeof(deep));
for(int i=; i<maxn; i++)
{
mp[i].clear();
}
} void dfs(int cur,int level)
{
deep[level]++;
for(int i=; i<mp[cur].size(); i++)
{
dfs(mp[cur][i],level+);
}
return;
} int main()
{
init();
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&x);
mp[x].push_back(i);
}
dfs(,);
int ans = ;
for(int i=; i<=n; i++)
{
if(deep[i]%)
ans++;
}
printf("%d\n",ans);
return ;
}

CodeForces - 930A Peculiar apple-tree(dfs搜索)的更多相关文章

  1. Codeforces 930A. Peculiar apple-tree (dfs)

    题目: 代码: #include <bits\stdc++.h> using namespace std; ]; //b[i]表示距离1号花絮i步的花絮的个数 map <int, l ...

  2. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  3. cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

      http://codeforces.com/contest/348/problem/B   B. Apple Tree time limit per test 2 seconds memory l ...

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

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

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

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

  6. ACM学习历程——POJ3321 Apple Tree(搜索,线段树)

          Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...

  7. POJ3321 - Apple Tree DFS序 + 线段树或树状数组

    Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...

  8. Codeforces 348B:Apple Tree(DFS+LCM+思维)

    http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点 ...

  9. POJ 3321 Apple Tree dfs+二叉索引树

    题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...

随机推荐

  1. 园艺研究生中途自学Java,赶上校招终进美团,分享面试经验

    前言 最近,圈子里的很多小伙伴都在面试,有些小伙伴儿拿到不错的offer,今天给大家推荐的这位小伙伴拿到美团点评的校招offer,他将自己这次面试的经历写下来供大家参考,看看你能回答多少? 背景 上海 ...

  2. mui中一级联动

    <!doctype html><html> <head> <meta charset="utf-8"> <title>& ...

  3. 黑客攻防技术宝典web实战篇:攻击用户·其他技巧习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 已知一项应用程序功能将一个查询字符串参数的内容插入到某个 HTTP 重定向的 Locati ...

  4. noip初赛

    运算符       联结词名称 异或 ⊕       非¬(-)     否定 与∧(·) 对应集合∩交集 对应按位与符号& 合取 或∨(+) 对应集合∪并集 对应按位或符号| 析取 条件→ ...

  5. stack(单调栈) POJ 2082 Terrible Sets

    题目传送门 题意:紧贴x轴有一些挨着的矩形,给出每个矩形的长宽,问能组成的最大矩形面积为多少 分析:用堆栈来维护高度递增的矩形,遇到高度小的,弹出顶部矩形直到符合递增,顺便计算矩形面积,且将弹出的宽度 ...

  6. EJB Timer Service is not available. Timers for application with id 95795415990861824 will not be deleted

    delete follows:glassfish\domains\domain1\applications\ejb-timer-service-appglassfish\domains\domain1 ...

  7. js去掉数组的空字符串

    后台返回数据的时候,有些数据为空时,一般都不进行显示,需要去除空字符串. 基本思路:获取数组张度,遍历数组,当数组某个值等于‘’或null或数据类型为undefined时,根据splice方法去除数据 ...

  8. AJPFX总结Collection集合(下)

    List集合特有方法 特有方法.凡是可以操作角标的方法都是该体系特有的方法. 增          add(index,element);在指定位置添加元素          addAll(index ...

  9. elasticsearch 2.4.0安装说明

    首先从官网下载安装包,是个压缩文件,然后解压 在es目录下找到es的配置文件 修改集群(cluster)名称 PS:一般情况下一台机只部署一个es程序,也就是一个集群,默认集群名是ewater_mai ...

  10. iOS 蒲公英第三方打包平台

    http://www.pgyer.com/doc/view/ios_install_failed