poj 3321:Apple Tree(树状数组,提高题)
Apple Tree
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree. The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka? Input The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree. Output For every inquiry, output the correspond answer per line.
Sample Input 3 Sample Output 3 Source POJ Monthly--2007.08.05, Huang, Jinsong
|
树状数组,提高题。
有些难度,要转一下弯。
题意:
一开始给你n-1个边,构成一颗苹果树,默认这个时候每个分叉上都有苹果。接下来有q个操作,这些操作分两种,要不有苹果摘走苹果,没苹果长出苹果,要不求某一分叉上所有的苹果个数。
思路:
关键是在每一个分叉处记录一个开始时间,和结束时间,表示dfs时经过这个分叉的时间和回到这个分叉的时间。这里的时间其实是dfs遍历次序的定位。这样就相当于有了一个区间,用树状数组就可以求出这个分叉点上的所有苹果的个数。
注意:
给你的n-1条边,是双向的,即a指向b,b也指向a,是无向图。
测试数据(来自poj讨论版):
Q
C
Q
Q
C
Q
C
Q
C
Q
ans:
代码:
#include <iostream>
#include <stdio.h>
using namespace std; #define MAXN 100010 int N;
int cnt=;
int c[MAXN];
int start[MAXN];
int end[MAXN]; struct Node{
int num;
Node* next; //孩子节点
Node()
{
next = NULL;
}
}tree[MAXN]; //临界表 int lowbit(int x)
{
return x & (-x);
} void add(int d,int x)
{
while(d<=N){
c[d] += x;
d += lowbit(d);
}
} int sum(int d)
{
int ans =;
while(d>=){
ans += c[d];
d -= lowbit(d);
}
return ans;
} void dfs(int v) //以r为根节点进行dfs遍历,返整个遍历之后的时间
{
start[v] = ++cnt;
Node* p = tree[v].next;
while(p){
if(start[p->num]==)
dfs(p->num);
p = p->next;
}
end[v] = cnt;
} void addedge(int a,int b) //在苹果树上加分支
{
Node* p = new Node;
p->num = b;
p->next = tree[a].next;
tree[a].next = p;
} int main()
{
int i,q;
scanf("%d",&N);
for(i=;i<N;i++){
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b);
addedge(b,a);
}
dfs(); for(i=;i<=N;i++) //初始化c[]
add(i,); scanf("%d",&q);
while(q--){ //q次操作
char cmd[];
int d;
scanf("%s%d",cmd,&d);
if(cmd[]=='C'){
if(sum(start[d])-sum(start[d]-)==)
add(start[d],-);
else
add(start[d],);
}
else if(cmd[]=='Q'){
printf("%d\n",sum(end[d])-sum(start[d]-));
}
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 3321:Apple Tree(树状数组,提高题)的更多相关文章
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
- POJ 3321 Apple Tree 树状数组+DFS
题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...
- 3321 Apple Tree 树状数组
LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- E - Apple Tree(树状数组+DFS序)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...
- POJ 3928 Ping pong 树状数组模板题
開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...
随机推荐
- bug-android之INSTALL_FAILED_NO_MATCHING_ABIS
bug描述: 经常在网络上下载一些实例,自己研究 ,运行时不时会出现这个bug: Installation error: INSTALL_FAILED_NO_MATCHING_ABIS bug解决方案 ...
- 解决html5 video不能播放 能播放声音不能播放视频
<video id="playVideo" style="width:90%; height:auto;" controls poster=". ...
- ndk学习19: 使用Eclipse调试so
1. 设置调试选项 在AndroidManifest文件加入允许调试 android:debuggable="true" 此时编译项目会多出: 2. 配置调试代码 把需要调 ...
- 把strassen乘法调出来了...
完美... 指针搞死我了 /// /// Author: zball /// No rights reserved /// (Creative Commons CC0) /// #include &l ...
- discuzX3后台管理插件开发示例一 用户表查询
上次的入门已经介绍了后台管理插件开发的基本步骤,下面简单写一个示例查询一下用户表 需要已完成以下操作: 1.已创建test后台管理插件 //详见 http://www.cnblogs.com/savo ...
- 【IDEA】IDEA 如何设置编辑器字体大小
intellij idea 如何更改编辑器文本字体和大小 换上了intellij idea之后,第一件事就是想要改变下文字字体,因为在我这个27寸的2k分辨率的屏幕上,文字显然太小了. intel ...
- POJ 1979
这是一道比较水的DPS的题目 题意就是求你可以走到的黑色的地板砖的块数,@代表你的起点,也是黑色的地板砖,#代表白色的,则说明你不能走,这就是一个广搜的题目 思路也很简单,如果你周围的那块地板是黑色的 ...
- nyoj_34_韩信点兵
中国剩余定理: 代码: #include <iostream> #include <cstdio> using namespace std; int main() { int ...
- Jquery 实现 “下次自动登录” 记住用户名密码功能
转载自:http://blog.csdn.net/aspnet_lyc/article/details/12030039?utm_source=tuicool&utm_medium=refer ...
- 【编程题目】n 个数字(0,1,…,n-1)形成一个圆圈,从数字 0 开始
第 18 题(数组):题目:n 个数字(0,1,…,n-1)形成一个圆圈,从数字 0 开始,每次从这个圆圈中删除第 m 个数字(第一个为当前数字本身,第二个为当前数字的下一个数字).当一个数字删除后, ...