zoukankan      html  css  js  c++  java
  • POJ-3321 Apple Tree 【DFS序+树状数组】

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

    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


    思路:
    dfs序,将每个结点转化成区间,将树形结构表示为线性结构,再利用线段树或者树状数组维护。

    Code:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<vector>
     5 using namespace std;
     6 #define INF 0x3f3f3f3f
     7 #define M(a, b) memset(a, b, sizeof(a))
     8 const int maxn = 1e5 + 5;
     9 int C[maxn], in[maxn<<2], out[maxn<<2], head[maxn<<1], n, id, cnt;
    10 bool ap[maxn];
    11 struct Edge {
    12     int to, next;
    13 }e[maxn<<1];
    14 
    15 void insert(int u,int v) {
    16     e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
    17     e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;
    18 }
    19 
    20 void dfs(int x, int fa) {
    21     in[x] = ++id;
    22     for (int i = head[x]; i; i = e[i].next) {
    23         int v = e[i].to;
    24         if (v == fa) continue;
    25         dfs(v, x);
    26     }
    27     out[x] = id;
    28 }
    29 
    30 int lowbit(int x) {return x & -x;}
    31 
    32 void update(int x, int d) {
    33     while (x <= n) {
    34         C[x] += d; x += lowbit(x);
    35     }
    36 }
    37 
    38 int sum(int x) {
    39     int ret = 0;
    40     while (x) {
    41         ret += C[x]; x -= lowbit(x);
    42     }
    43     return ret;
    44 }
    45 
    46 int main() {
    47     ios::sync_with_stdio(false);
    48     while (cin >> n) {
    49         memset(C, 0, sizeof(C));
    50         int a, b;
    51         for (int i = 1; i <= n; ++i) {
    52             ap[i] = 1;
    53             update(i, 1);
    54         }
    55         for (int i = 1; i < n; ++i) {
    56             cin >> a >> b;
    57             insert(a, b);
    58         }
    59         id = 0;
    60         dfs(1, 0);
    61         char op[3];
    62         int m, q;
    63         cin >> m;
    64         while (m--) {
    65             cin >> op;
    66             cin >> q;
    67             if (op[0] == 'C') {
    68                 if (ap[q]) update(in[q], -1);
    69                 else update(in[q], 1);
    70                 ap[q] ^= 1;
    71             }
    72             else {
    73                 cout << sum(out[q]) - sum(in[q]-1) << endl;
    74             }
    75         }
    76     }
    77 
    78     return 0;
    79 }
  • 相关阅读:
    [题解]北京2018
    [数据结构][字典树]Word Puzzles
    [数据结构][字典树]Hardwood Species
    [数学][广义欧拉定理]上帝与集合的正确用法
    Equal Sums
    Useful Decomposition
    网络流 EK算法
    线段树各类操作
    唯一分解定理
    Kuro and Walking Route
  • 原文地址:https://www.cnblogs.com/robin1998/p/6539591.html
Copyright © 2011-2022 走看看