zoukankan      html  css  js  c++  java
  • POJ 3321 Apple Tree

    题目链接http://poj.org/problem?id=3321

    解题思路:dfs加时间戳然后简单树状数组单点更新区间查询即可。

    代码:

     1 const int maxn = 1e5 + 5;
     2 struct Edge{
     3     int to, next;
     4 };
     5 Edge edges[maxn];
     6 int head[maxn], tot;
     7 int st[maxn], ed[maxn], cnt;
     8 int bit[maxn], status[maxn], n, m;
     9 
    10 void init(){
    11     memset(head, -1, sizeof(head));
    12     memset(st, 0, sizeof(st));
    13     memset(ed, 0, sizeof(ed));
    14     memset(status, 0, sizeof(status));
    15     cnt = tot = 0;
    16 }
    17 void addEdge(int u, int v, int w){
    18     edges[tot].to = v;
    19     edges[tot].next = head[u];
    20     head[u] = tot++; 
    21 }
    22 void dfs(int u){
    23     cnt++;
    24     st[u] = cnt;
    25     for(int i = head[u]; i != -1; i = edges[i].next){
    26         dfs(edges[i].to);
    27     }
    28     ed[u] = cnt;
    29 }
    30 void add(int i, int x){
    31     while(i <= cnt){
    32         bit[i] += x;
    33         i += i & (-i);
    34     }
    35 }
    36 int sum(int i){
    37     int ans = 0;
    38     while(i > 0){
    39         ans += bit[i];
    40         i -= i &(-i) ;
    41     }
    42     return ans;
    43 }
    44 
    45 int main(){
    46     scanf("%d", &n);
    47     init();
    48     for(int i = 1; i < n; i++){
    49         int u, v;
    50         scanf("%d %d", &u, &v);
    51         addEdge(u, v, 0);
    52     }
    53     dfs(1);
    54     for(int i = 1; i <= cnt; i++){
    55         add(i, 1);
    56     }
    57     scanf("%d", &m);
    58     while(m--){
    59         char ch;
    60         int x;
    61         scanf(" %c %d", &ch, &x);
    62         if(ch == 'C'){
    63             if(status[st[x]] == 0){
    64                 status[st[x]] = 1;
    65                 add(st[x], -1);
    66             }
    67             else{
    68                 status[st[x]] = 0;
    69                 add(st[x], 1);
    70             }
    71         }
    72         else{
    73             int ans = sum(ed[x]) - (st[x] == 1? 0: sum(st[x] - 1));
    74             printf("%d
    ", ans);
    75         }
    76     }
    77 }

    题目:

    Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 30438   Accepted: 9091

    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
    

    Source

    POJ Monthly--2007.08.05, Huang, Jinsong
  • 相关阅读:
    进度三
    centos7 安装nodejs 最新版及yarn安装
    sed -i 增加或删减注释#
    解决go编译报错../github.com/golang/freetype/truetype/face.go:13:2: cannot find package "golang.org/x/image/font" in any of:
    解决go打包时报错 models/util/code.go:23:2: cannot find package "github.com/boombuler/barcode" in any of:
    Dockerfile文件说明
    docker exec 报错:error: code = 13 desc = invalid header field value "oci runtime error
    Docker中 /var/lib/docker/overlay2/磁盘爆满,如何解决?
    CentOS7默认安装的/home中转移空间到根目录/
    Docker存储驱动之--overlay2
  • 原文地址:https://www.cnblogs.com/bolderic/p/7293904.html
Copyright © 2011-2022 走看看