POJ 3321 Apple Tree 题解

POJ 3321

题目

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?


输入

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
“C 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
“Q 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


输出

For every inquiry, output the correspond answer per line.


样例

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

output
3
2


题意


解题思路

做一个dfsdfsdfs序
存储以iii为根的子节点区间
树状数组存苹果数
统计时要记得左边界减1


代码

#include<iostream>
#include<cstdio>
using namespace std;
struct hhx{int to,next;
}a[300010];
char s;
int n,m,x,y,t,head[100020],f[100010][3],b[100010],c[100020];
void add(int x,int y)  //连接,建边
{a[++t].to=y;a[t].next=head[x];head[x]=t;
}
void dfs(int d)  //求dfs序
{t++;f[d][1]=t;for (int i=head[d];i;i=a[i].next)dfs(a[i].to);f[d][2]=t;
}
void add2(int x)  //修改苹果数
{c[x]=c[x]*-1;int sum=c[x];x=f[x][1]; for (;x<=n;x+=x & (-x))b[x]+=sum;
}
int tj(int x)
{int sum=0;for (;x>0;x-=x & (-x))sum+=b[x];return sum;
}
int main()
{scanf("%d",&n);for (int i=1;i<n;i++){scanf("%d%d",&x,&y);add(x,y);}t=0;dfs(1);for (int i=1;i<=n;i++)  //初值,每一个点都有苹果{c[i]=-1;add2(i);}scanf("%d",&m);for (int i=1;i<=m;i++){cin>>s;scanf("%d",&x);if (s=='C') add2(x);//单点修改苹果else printf("%d\n",tj(f[x][2])-tj(f[x][1]-1));  //以x为根的区间和}return 0;
}

POJ 3321 Apple Tree 题解相关推荐

  1. 【线段树_DFS序】POJ 3321 Apple Tree

    POJ 3321 Apple Tree 题意:给出一个N个结点的无根树.但是要将1看作根,也相当于是一个有根树.最初每个结点都有一个苹果.有两种操作,Q x: 以x为根的子树上有多少个苹果:C x: ...

  2. POJ 3321 Apple Tree【树状数组】

    题目 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tr ...

  3. poj 3321 Apple Tree(dfs序+树状数组求和模型)

    题目链接:http://poj.org/problem?id=3321 解题思路: 先dfs求出序列,将子树转化到dfs序列的区间内,接下来就是简单的树状数组求和模型了.水题. #include< ...

  4. poj 3321 Apple Tree 树状数组

    http://poj.org/problem?id=3321 一棵树,开始时每个结点都有一个苹果,输入C x表示更新x结点,若x结点有苹果,把该结点苹果摘掉,若该节点无苹果,在该节点上增加一个新的苹果 ...

  5. poj 3321 Apple Tree

    树状数组 题意:一个树,以树枝连接两个点的形式给出,固定以1为整棵树的根.苹果长在树的节点上,节点上只可能0或1个苹果,一开始每个节点都有1个苹果 有两种操作,C表示更改某个节点的苹果数,0变1,1变 ...

  6. poj 3321 Apple Trie

    /*poj 3321 Apple Trie这道题的关键是如何将一个树建成一个一维数组利用树状数组来解题!可以利用dfs()来搞定,我们在对一个节点深搜后,所经过的节点的数目就是该节点的子树的数目所以我 ...

  7. 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

    题干: There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the t ...

  8. 【POJ - 2486】Apple Tree (树形背包,dp)

    题干: Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There ...

  9. Apple Tree(树状数组+dfs序+邻接表数组(链式前向星) )

    链接:http://poj.org/problem?id=3321 Description There is an apple tree outside of kaka's house. Every ...

最新文章

  1. 【编译原理】递归下降的预测分析(真の能看懂~!)
  2. 信号与系统:快速傅里叶变换FFT中的实际频率(奈奎斯特频率解析)
  3. 已解决:Ubuntu16.4和Windows10创建共享文件夹
  4. 科技论文中的分析与综合-如何写好科技论文之我见(七)
  5. struts2 处理请求流程分析(结合源码)
  6. redis之sorted sets类型及操作
  7. win7 其他用户当前已登录到此计算机,win7旗舰版的电脑如何切换至公共用户?-win7用户,win7账户已被停用按f8没反应...
  8. 【BZOJ】1013: [JSOI2008]球形空间产生器sphere(高斯消元)
  9. IDEA 2021.1正式发布,新增了这几个超实用功能!
  10. auth java_java – 使用auth的httpget请求
  11. python怎么改变button的颜色_python – 改变Kivy中Button的背景颜色
  12. 火爆全网MySQL路线笔记!java静态变量和实例变量
  13. 设计模式(7) 续原型模式
  14. FLEX AIR添加系统托盘图标步骤
  15. 【转】 TechED2010与我(三) —— 初识云计算
  16. 1265=马拦过河卒
  17. Python全栈之路--Django ORM详解
  18. Py之Scipy:Scipy库(高级科学计算库)的简介、安装、使用方法之详细攻略
  19. matlab中转置操作,如何转置MATLAB表?
  20. 极通EWEBS 常见问题及其解决办法

热门文章

  1. android 新浪微博Oauth认证
  2. Linux下使用vsftpd架设FTP服务器过程
  3. 爱贝壳 教研云 动画执行流程和实现猜测
  4. 利用matplotlib绘制条形图,直观呈现2019年电影票房数据
  5. 软文推广中如何选定关键词?
  6. ORACLE 千万条的数据优化
  7. 两组回归系数差异检验_科学网-如何检验两组回归系数之间的差异显著性?-李国强的博文...
  8. 统计 flv视频总时长
  9. 浏览器前进与后退的秘密——栈 (栈的理解与实现)
  10. python如何去掉list中的中括号、二级列表中的括号、双引号