题目:

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

思路:

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. hdoj1394

    题意还告诉我们是0-n-1之间的数,那么我们每次把一个数放到后面去,求一下比他大的,还有比他小的: 比如: 1 3 6 9 0 8 5 7 4 2 逆序数num:22 3 6 9 0 8 5 7 4 ...

  2. IT兄弟连 JavaWeb教程 Servlet表单乱码问题

    在使用原生的Servlet进行Web应用开发时经常会遇到一些中文乱码问题,造成乱码问题的原因只有一个:即客户端与服务端的字符编码不一致所导致的. 请求参数乱码问题 服务器端获取客户端传递过来的数据出现 ...

  3. 交表(Send a Table)

    #include<stdio.h> #include<string.h> #define N 50010 int phi[N],n,sum[N]; void phi_table ...

  4. sybase修改默认字符集为cp936

    原文地址:http://blog.sina.com.cn/s/blog_4d6854860100xn3f.html 报错信息:2402 error converting characters into ...

  5. queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

    题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...

  6. python对ini配置文件处理

    实例文件: [root@docker2 ~]# cat test.ini [base] host = 192.168.88.121 port = 3306 user = root path = /ho ...

  7. Win7下单机版的伪分布式solrCloud环境搭建Tomcat+solr+zookeeper【转】

    Win7下单机版的伪分布式solrCloud环境搭建Tomcat+solr+zookeeper 1.软件工具箱 在本文的实践中,需要用到以下的软件: Tomcat-7.0.62+solr-5.0.0+ ...

  8. IIS伪静态失效

    故障描述: 今天有个美国VPS的用户向我们救助,说他的网站昨晚还好好的,早上起来就发现404了,但是后台却可以正常登陆.经过我们检查后发现原来是伪静态失效了,查看日志,发现许多ISAPI_Rewrit ...

  9. 《Python基础教程》 读书笔记 第五章(下)循环语句

    5.5.1while循环 x=1 while x<=100: print x x+=1 确保用户输入了名字: name="" while not name: name=raw ...

  10. elf 文件

    objdump  文件名 readelf   文件名