Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 36442   Accepted: 10894

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 题意:给出了一棵树,树有很多的树杈,标号分别为1-n,如上图所示,初始状态时,每个的树杈位置都有一个苹果,有m个操作,操作(c num)是如果num号树杈有苹果,就摘掉,否则这个树杈上就会长出苹果;操作(Q num)表示求以第num个树杈为根的子树共有多少苹果。
思路:这题可以用dfs序给树杈重新标号,使得每一棵子树的序号都是连续的,然后就将树状结构转换为了线性结构,再使用树状数组求和就是求子树的所有苹果数了。 DFS序粗讲:所谓dfs序就是一棵树上每个节点在dfs先序遍历中的进出栈的时间序列。如下面这棵树:
 

它的dfs序就是:

上面的遍历结果中每个点都出现两次,因为一次是进栈,一次出栈。

我们可以发现,以每个节点作为根节点的子树,这棵子树中的所有节点在dfs序中都处于根节点中间。

例如:以B为根节点的子树,有BEFK四个点,而在dfs序中,这四个点的遍历顺序是相连的,为BEEFKKFB,EFK作为子节点,遍历的顺序在B进栈之后,而又在B出栈之前。验证可以发现,每一个节点都满足这一性值。

所以,我们按照dfs序重新给各个节点编号,每个节点记录两个值,一个是进栈的时间,一个是出栈的时间。如下图:

这棵树按dfs先序遍历之后获得了如上图的编号,每个点都有其进栈的时间和出栈的时间,两者形成了一个区间,图中每个点的进栈时间都不同,我们就以进栈时间点作为新的编号(根据写法不同也可以是出栈时间各不同)。我们可以发现,若以某个节点作为根节点,那么它的子树上的所有点的的编号都在根节点的区间范围内。例如上图中的点4,它的新编号为5,区间5-7,它的两个子节点编号分别为6,7;节点2的编号为2,区间为2-3,子节点新编号为3(旧编号为5)。

知道这一点之后,我们就可以来求以某个节点为根的子树的值的和了。比如求上图中4号节点为根的子树的和,则就是求新编号区域为5-7的和,这是连续的,用树状数组就行了,也即是sum(7)-sum(4)。

参考博客:https://blog.csdn.net/qq_39670434/article/details/78425125

具体操作看代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<stack>
#include<climits>
#include<map>
#include<queue>
#define eps 1e-7
#define ll long long
#define inf 0x3f3f3f3f
#define pi 3.141592653589793238462643383279
using namespace std;
const int MAXN = 1e5+; struct Edge{
int next,to;
}edge[MAXN];
int head[MAXN],n,m,cnt,in[MAXN],out[MAXN],visit[MAXN],c[MAXN]; void ADD(int beg,int end) //链式前向心建树
{
edge[cnt].next = head[beg];
edge[cnt].to = end;
head[beg] = cnt++;
} void DFS(int u) //获得dfs序
{
visit[u] = ; //标记节点已经被范围(此题可以没有)
in[u] = ++cnt; //记录节点u进栈的时间
for(int i=head[u]; i!=-; i=edge[i].next) //遍历节点u所有的子节点
{
int j = edge[i].to;
if(!visit[j]) DFS(j); //搜索子节点
}
out[u] = cnt; //记录节点u出栈的时间
} int lowBit(int x)
{
return x&(-x);
} void add(int x,int num)
{
for(int i=x; i<=n; i+=lowBit(i))
c[i] += num;
} int query(int x)
{
int ans = ;
for(int i=x; i>; i-=lowBit(i))
ans += c[i];
return ans;
} int main()
{
int t;
char ques[];
while(scanf("%d",&n)!=EOF)
{
fill(head,head+n+,-);
fill(visit,visit+n+,);
cnt = ; int beg,end;
for(int i=; i<n-; ++i)
{
scanf("%d%d",&beg,&end);
ADD(beg,end);
}
cnt = ;
DFS(); //dfs获得新的编号 c[] = ;
for(int i=; i<=n; ++i) add(i,); //树状数组给每个节点赋值,因为题目初始状态是每个节点都有苹果 cin>>m;
while(m--)
{
scanf("%s%d",ques,&t);
if(ques[] == 'C')
{
if(visit[t] == ) //判断此节点是否有苹果
{ add(in[t],-); //有苹果就减去
visit[t] = ; //然后标记为没有苹果
}
else
{
add(in[t],); //没有苹果就加上
visit[t] = ; //然后标记为有
}
}
else
{
int ans = query(out[t]) - query(in[t]-); //计算子树的苹果数
printf("%d\n",ans);
}
}
}
return ;
}
												

poj3321-Apple Tree(DFS序+树状数组)的更多相关文章

  1. [poj3321]Apple Tree(dfs序+树状数组)

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

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

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

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

  5. POJ3321Apple Tree Dfs序 树状数组

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

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

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

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

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

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

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

  10. 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. 基元线程同步构造之waithandle中 waitone使用

    在使用基元线程同步构造中waithandle中waitone方法的讲解: 调用waithandle的waitone方法阻止当前线程(提前是其状态为Nonsignaled,即红灯),直到当前的 Wait ...

  2. oracle 网络环境配置

    PLSQL Developer连接Oracle11g 64位数据库配置详解 最近换了台64bit的电脑,所以oracle数据库也跟着换成了64bit的,不过 问题也随之产生,由于plsql devel ...

  3. SciTE: 中文字符支持问题

    SciTE: 中文字符支持问题   SciTE(Scintilla Text Editor)是一个体积小巧的文本编辑器. 但是它默认的设置对中文字符处理不好,其实只要对它进行相应的配置,就可以了. 1 ...

  4. js 获取input选择的图片的信息

    1JS $("#btn").click(function () { var imageEle = document.getElementById("images" ...

  5. date.getTime()

    Date date = new Date(); System.out.println(date.getTime()); 输出结果是1210745780625 编译时间当时时间大概是2008年5.14好 ...

  6. 人机大战中AlphaGo及其执子人黄士杰

    2016年3月9日注定要写入围棋界的历史.IT界的历史以及科学界的历史.当天,韩国著名围棋棋手李世石VS谷歌AlphaGo的人机大战赛在韩国首尔举行.对弈的一方为拥有1200多个处理器的谷歌人工智能系 ...

  7. Java按值传递、按引用传递

    一般我们会说Java基本类型采用值传递,对象以及数组采用引用传递.但事实上这只是表面上的现象.实质上,Java都是按值传递引用.(Java中“引用”的概念相当于C++中的指针,可以不断改变值) 一,对 ...

  8. kwic--Java统计单词个数并按照顺序输出

    2016-07-02(随笔写作时间) 写了好久的程序了为了避免以后用到.......... 是一个统计单词个数,并按照个数从大到小输出的.输入文件名OK 了 单词是按照首字母排序的,,,里面用到映射等 ...

  9. vc通过webbrowser操作ie元素

    1>需要引用 webbrowser2.h,mshtml.h //m_web绑定的webbrowser的变量 CComQIPtr<IHTMLDocument2,&IID_IHTMLD ...

  10. javascript正则表达式验证密码(必须含数字字符特殊符号,长度4-16位之间)

    var newpwd = $("#newpassword").val(); //var pattern = "([A-Za-z]|[0-9]|-|_){4,16}&quo ...