poj3321-Apple Tree(DFS序+树状数组)

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

Descriptionios

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

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?ide

 

 

Inputui

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 beginningspa

Output.net

For every inquiry, output the correspond answer per line.

Sample Input3d

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

Sample Outputcode

3
2

题意:给出了一棵树,树有不少的树杈,标号分别为1-n,如上图所示,初始状态时,每一个的树杈位置都有一个苹果,有m个操做,操做(c num)是若是num号树杈有苹果,就摘掉,不然这个树杈上就会长出苹果;操做(Q num)表示求以第num个树杈为根的子树共有多少苹果。
思路:这题能够用dfs序给树杈从新标号,使得每一棵子树的序号都是连续的,而后就将树状结构转换为了线性结构,再使用树状数组求和就是求子树的全部苹果数了。

DFS序粗讲:所谓dfs序就是一棵树上每一个节点在dfs先序遍历中的进出栈的时间序列。以下面这棵树:
 

它的dfs序就是:blog

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

咱们能够发现,以每一个节点做为根节点的子树,这棵子树中的全部节点在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

具体操做看代码:

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<string>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<stack>
  8 #include<climits>
  9 #include<map> 
 10 #include<queue>
 11 #define eps 1e-7
 12 #define ll long long
 13 #define inf 0x3f3f3f3f
 14 #define pi 3.141592653589793238462643383279
 15 using namespace std;
 16 const int MAXN = 1e5+7;
 17 
 18 struct Edge{
 19     int next,to;
 20 }edge[MAXN];
 21 int head[MAXN],n,m,cnt,in[MAXN],out[MAXN],visit[MAXN],c[MAXN];
 22 
 23 void ADD(int beg,int end) //链式前向心建树 
 24 {
 25     edge[cnt].next = head[beg];
 26     edge[cnt].to = end;
 27     head[beg] = cnt++;
 28 }
 29 
 30 void DFS(int u) //得到dfs序 
 31 {
 32     visit[u] = 1; //标记节点已经被范围(此题能够没有) 
 33     in[u] = ++cnt; //记录节点u进栈的时间 
 34     for(int i=head[u]; i!=-1; i=edge[i].next) //遍历节点u全部的子节点 
 35     {
 36         int j = edge[i].to;  
 37         if(!visit[j]) DFS(j); //搜索子节点 
 38     }
 39     out[u] = cnt; //记录节点u出栈的时间 
 40 }
 41 
 42 int lowBit(int x)
 43 {
 44     return x&(-x);
 45 }
 46 
 47 void add(int x,int num)
 48 {
 49     for(int i=x; i<=n; i+=lowBit(i))
 50         c[i] += num;
 51 }
 52 
 53 int query(int x)
 54 {
 55     int ans = 0;
 56     for(int i=x; i>0; i-=lowBit(i))
 57         ans += c[i];
 58     return ans;
 59 }
 60 
 61 int main()
 62 {
 63     int t;
 64     char ques[2];
 65     while(scanf("%d",&n)!=EOF)
 66     {
 67         fill(head,head+n+5,-1);
 68         fill(visit,visit+n+5,0);
 69         cnt = 0;
 70         
 71         int beg,end;
 72         for(int i=0; i<n-1; ++i)
 73         {
 74             scanf("%d%d",&beg,&end);
 75             ADD(beg,end);
 76         }
 77         cnt = 0;
 78         DFS(1); //dfs得到新的编号 
 79 
 80         c[0] = 0;    
 81         for(int i=1; i<=n; ++i) add(i,1); //树状数组给每一个节点赋值,由于题目初始状态是每一个节点都有苹果 
 82         
 83         cin>>m;
 84         while(m--)
 85         {
 86             scanf("%s%d",ques,&t);
 87             if(ques[0] == 'C')
 88             {
 89                 if(visit[t] == 1) //判断此节点是否有苹果 
 90                 {
 91                     
 92                     add(in[t],-1); //有苹果就减去 
 93                     visit[t] = 0; //而后标记为没有苹果 
 94                 }
 95                 else
 96                 {
 97                     add(in[t],1); //没有苹果就加上 
 98                     visit[t] = 1;  //而后标记为有 
 99                 }
100             }
101             else
102             {
103                 int ans = query(out[t]) - query(in[t]-1); //计算子树的苹果数 
104                 printf("%d\n",ans);
105             }
106         }
107     }
108     return 0;
109 }
相关文章
相关标签/搜索