zoukankan      html  css  js  c++  java
  • POJ3321 Apple Tree

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 37048   Accepted: 11048

    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 #include <iostream>
     2 using namespace std;
     3 
     4 #define maxn 210100 //注意:maxn设得太大会导致RE
     5 int head[maxn], a[maxn], C[maxn], cnt, tot, N;
     6 
     7 struct fork
     8 {
     9     int l, r; //fork代表交叉点,左值为该分叉点的序号(按访问顺序),r为该分叉点以上的最大的序号
    10 }f[maxn];//index为分叉点的编号(题给)
    11 
    12 struct edge
    13 {
    14     int to, next; //to为指向的分叉点序号,next指向下一条树枝
    15 }e[maxn];
    16 
    17 void add(int u, int v)
    18 {
    19     cnt++;
    20     e[cnt].to = v;
    21     e[cnt].next = head[u];
    22     head[u] = cnt;
    23 }
    24 
    25 int lowbit(int x)
    26 {
    27     return x & (-x);
    28 }
    29 
    30 void dfs(int x)
    31 {
    32     //cout << "x: " << x << endl;
    33     f[x].l = ++tot;
    34     //cout << "tot: " << tot << endl;
    35     for (int i = head[x]; i; i = e[i].next)
    36         dfs(e[i].to);
    37     f[x].r = tot;
    38 }
    39 
    40 void change(int x)
    41 {
    42     for (int i = x; i <= N; i += lowbit(i)) {  //维护树状数组信息,复杂度O(logN)
    43         if (a[x] == 1) C[i]++;
    44         else C[i]--;
    45     }
    46 }
    47 
    48 int sum(int x)
    49 {
    50     int re = 0;
    51     for (int i = x; i > 0; i -= lowbit(i))//树状数组求和,复杂度O(logN)
    52         re += C[i];
    53     return re;
    54 }
    55 
    56 int main()
    57 {
    58     int M, u, v;
    59     char query;
    60     cin >> N;
    61     for (int i = 1; i < N; i++) {
    62         cin >> u >> v;
    63         add(u, v);
    64     }
    65 
    66     dfs(1);
    67 
    68     for (int i = 1; i <= N; i++) {
    69         a[i] = 1;
    70         change(i);
    71     }
    72 
    73     cin >> M;
    74     while (M--) {
    75         cin >> query >> u;
    76         if (query == 'C') {
    77             a[f[u].l] = (a[f[u].l] + 1) % 2;//注意:并不是a[u],因为C是按访问顺序求和的
    78             change(f[u].l);
    79         }
    80         if (query == 'Q') {
    81             //cout << f[u].r << endl;
    82             //cout << sum(f[u].r) << endl;
    83             cout << sum(f[u].r) - sum(f[u].l - 1) << endl;
    84         }
    85     }
    86 
    87     return 0;
    88 }
  • 相关阅读:
    《jQuery实战(第二版)》读书笔记
    软件测试对于代码安全的诸多事宜
    关于Maven的安装及初步使用
    Windows下GIT安装与使用(上传远程端)
    白盒测试
    关于VS2013的编码的UI测试。
    闰年检验
    等价类划分例子中的些许添加
    软件测试技术第二周课堂等价类习题
    软件测试技术第一周课堂随笔记录
  • 原文地址:https://www.cnblogs.com/Jeffrey-Y/p/9765003.html
Copyright © 2011-2022 走看看