CodeForces - 930A Peculiar apple-tree(dfs搜索)
题目:
给出一个树,这棵树上每个结点每一秒都会结出一颗果实,果实每经过一秒就会落向下一个结点,如果一个结点在同一时刻上的果实两两抵消,问最后在根节点处一共有多少个果实。
思路:
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搜索)的更多相关文章
- Codeforces 930A. Peculiar apple-tree (dfs)
题目: 代码: #include <bits\stdc++.h> using namespace std; ]; //b[i]表示距离1号花絮i步的花絮的个数 map <int, l ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- cf202-div 1-B - Apple Tree:搜索,数论,树的遍历
http://codeforces.com/contest/348/problem/B B. Apple Tree time limit per test 2 seconds memory l ...
- 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 ...
- ACM学习历程——POJ3321 Apple Tree(搜索,线段树)
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...
- 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 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
随机推荐
- Jquery下拉框左右选择
1.说明 本文demo实现下拉框左右选择,本文地址:http://www.cnblogs.com/lengzhan/p/6423023.html 2.代码 <!DOCTYPE html PUBL ...
- AFN清除图片缓存 以及菊花转圈
AFNetworking网络库已经提供了很好的图片缓存机制,效率是比较高的,但是我发现没有直接提供清除缓存的功能,可项目通常都需要添加 清除功能的功能,因此,在这里我以UIImageView+AFNe ...
- HDU - 6066 RXD's date
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6066 #include <iostream> using namespace ...
- Query on a tree IV SPOJ - QTREE4
https://vjudge.net/problem/SPOJ-QTREE4 点分就没有一道不卡常的? 卡常记录: 1.把multiset换成手写的带删除堆(套用pq)(作用很大) 2.把带删除堆里面 ...
- Helga Hufflepuff's Cup CodeForces - 855C
Helga Hufflepuff's Cup CodeForces - 855C 题意:给一棵n个节点的树,要给每一个节点一个附加值,附加值可以为1-m中的一个整数.要求只能有最多x个节点有附加值k. ...
- 1-13Object类之toString方法
Object中的toString方法 SUN在Object类中设计toString方法的目的:返回java对象的字符串表示形式. 在现实的开发过程中,Object中的toString方法就是要被重写的 ...
- hdu 1979 DFS + 字典树剪枝
http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...
- CentOS 7不重启刷新磁盘列表
[root@master-09:29:09 33~]#ls /sys/class/scsi_host/host0 host1 host2[root@master-09:29:55 34~]#echo ...
- JS执行保存在数据库中的JS代码
function createScript(script) { var myScript = document.createElement("script"); myScript. ...
- CentOS6.5下安装Redis2.8.6和phpredis2.2.4扩展
一.版本说明 CentOS版本 [plain]view plaincopyprint? [root@localhost ~]# uname Linux [root@localhost ~]# unam ...