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

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.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output

For every inquiry, output the correspond answer per line.

Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1

Sample Output

3
2

Source

POJ Monthly--2007.08.05, Huang, Jinsong
 
继续继续继续
 
dfs序嘛
原来之前理解的是错的
其实就是tarjan的dfn与low数组
得出dfn与low后
dfn[i]是起点,low[i]是终点,中间的一段就是点i的子树了
这样dfs序就可以“方便的维护子树”
而对于大部分按照dfs序维护序列的题,与树状数组是相当好的组合
这道题差不多是裸题了,多开一个表示当前节点状态的数组就可以了
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int ap[],bit[];
int bg[],ed[],cnt=;
int n;
typedef struct{
int to,nxt;
}edge;
edge gra[];
int head[],num=;
int add(int frm,int to){
gra[++num].nxt=head[frm];
gra[num].to=to;
head[frm]=num;
return ;
}
int dfs(int u,int fa){
bg[u]=++cnt;
int j;
for(j=head[u];j;j=gra[j].nxt){
if(gra[j].to!=fa)dfs(gra[j].to,u);
}
ed[u]=cnt;
return ;
}
int lb(int x){
return x&(-x);
}
int c(int x){
int num=ap[x];
x=bg[x];
while(x<=n){
bit[x]+=num;
x+=lb(x);
}
return ;
}
int q(int x){
int a1=,a2=;
int e=ed[x];
while(e){
a1+=bit[e];
e-=lb(e);
}
int b=bg[x]-;
while(b){
a2+=bit[b];
b-=lb(b);
}
return a1-a2;
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int x,y;
scanf("%d %d",&x,&y);
add(x,y);
add(y,x);
}
dfs(,);
for(int i=;i<=n;i++){
ap[i]=;
c(i);
}
int m;
scanf("%d",&m);
for(int i=;i<=m;i++){
char in[];
int x;
scanf("%s %d",in,&x);
if(in[]=='C'){
ap[x]*=-;
c(x);
}
else printf("%d\n",q(x));
}
return ;
}

睡觉

[poj3321]Apple Tree(dfs序+树状数组)的更多相关文章

  1. POJ 3321 Apple Tree DFS序 + 树状数组

    多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...

  2. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  3. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  4. POJ3321Apple Tree Dfs序 树状数组

    出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...

  5. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

  6. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  8. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  9. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

随机推荐

  1. 带清空按钮的EditText

    public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { // 删除按钮的 ...

  2. JDBC 基本操作

    1. 简介 JDBC(Java DataBase Connectivity) 是有一些接口和类构成的API JDBC是J2SE的一部分, 又java.sql: javax.sql包组成.   应用程序 ...

  3. 小吐槽Toolbar

    最近弄界面 要吧全部图标改成PNG格式 虽说从2010以后Delphi默认支持PNG格式图片, 但是想应用到按钮上, 似乎除了TButton意外, 也只能ToolBar可以正常显示了, 其他的, 比如 ...

  4. sublime添加PHP语法检查

    1.找到php文件目录 如E:\xampp\php 放到环境变量的path中   2.sublime 工具-编译系统-新编译系统  {     "cmd": ["php& ...

  5. 解决git .ignore文件无效

    在用 Git 进行代码管理的时候,我们会用 .gitignore 文件来描述哪些文件是不需要进行版本管理的,也就是被忽略掉. 如果我们在第一次提交的时候,忘记添加 .gitignore 文件或者在首次 ...

  6. Markdown资源 markd

    markdown是将某些格式的文本,翻译成HTML的一个库,非常精妙! https://www.npmjs.com/package/markedhttps://github.com/adam-p/ma ...

  7. jqGrid通用编辑规则

    一个将数据显示在grid中主要的原因是为了快速容易的编辑它,jqGrid支持3种编辑方法 jqGrid单元格编辑配置,事件及方法::编辑表格中的单元格 jqGrid行编辑配置:同时编辑一行中的多个单元 ...

  8. 引入RequireJS(一)

    例子 http://www.utlcenter.com/user/index.aspx 一.文件引用 <script src="Js/require.js" defer as ...

  9. Android之下载管理者

    public interface HttpDownloader { public void setDownloadManager(HttpDownloadManager manager); publi ...

  10. Java类的基本运行顺序

    不看不知道,一看还真吓了一跳!Java类的基本运行顺序你真的知道吗?如果你有疑惑,那就看下面这篇文章吧.   原文出自:[url]http://column.ibeifeng.com/chenchen ...