• POJ 3321 Apple Tree


    树(不一定是2叉树)映射成数组特别麻烦,在网上看了好多。本来想用  vector< vector<int>  >,但太麻烦。最后用的是左兄弟右孩子的办法。把树建成后,再DFS一遍,记录时间戳『孩子中最小的 ,最大的(就是自己在树状数组中的位置)』。然后就能用树状是数组求和了。

                                                                      Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 15487   Accepted: 4595

    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

    31 21 33Q 1C 2Q 1

    Sample Output

    32

    Source

    POJ Monthly--2007.08.05, Huang, Jinsong
     
     
      1 #include <cstdio>
      2 #include <cstring>
      3 
      4 #define MAXN 100005
      5 
      6 using namespace std;
      7 
      8 typedef struct CH
      9 {
     10     int idx;
     11     CH* nxt;
     12 }CHILD;//孩子结点
     13 
     14 typedef struct N
     15 {
     16     int high;///idx
     17     int low;
     18     CHILD* child;
     19 }NOTE;
     20 
     21 NOTE fort[MAXN];
     22 int vis[MAXN];
     23 char apple[MAXN];
     24 int n,cnt;
     25 int tree[MAXN];
     26 
     27 void addNote(int x,int y)
     28 {
     29     CHILD* p;
     30     p=new CHILD;
     31     p->idx=y;
     32     p->nxt=fort[x].child;
     33     fort[x].child=p;
     34 }
     35 
     36 void DFS(int t)
     37 {
     38     CHILD* p;
     39     vis[t]=1;
     40     fort[t].low=cnt;
     41     p=fort[t].child;
     42     while(p)
     43     {
     44         if(!vis[p->idx])
     45             DFS(p->idx);
     46         p=p->nxt;
     47     }
     48     fort[t].high=cnt++;
     49 }
     50 ///************树状数组******************///
     51 int lowbit(int x)
     52 {
     53     return x&(-x);
     54 }
     55 
     56 int getSum(int x)
     57 {
     58     int sum=0;
     59     while(x>0)
     60     {
     61         sum+=tree[x];
     62         x-=lowbit(x);
     63     }
     64     return sum;
     65 }
     66 
     67 void Update(int pos,int val)
     68 {
     69     while(pos<MAXN)
     70     {
     71         tree[pos]+=val;
     72         pos+=lowbit(pos);
     73     }
     74 }
     75 
     76 int main()
     77 {
     78     memset(apple,1,sizeof(apple));
     79 
     80     int n,s,a,b;
     81     char ch[10];
     82     scanf("%d",&n);
     83     for(int i=1;i<n;i++)
     84     {
     85         scanf("%d %d",&a,&b);
     86         addNote(a,b);//加2遍
     87         addNote(b,a);
     88     }
     89 
     90     cnt=1;
     91     DFS(1);
     92 
     93     for(int i=1;i<=n;i++)
     94         tree[i]=lowbit(i);
     95 
     96     scanf("%d",&s);
     97     while(s--)
     98     {
     99         scanf("%s%d",ch,&a);
    100         if(ch[0]=='C')
    101         {
    102             if(apple[a]==1)
    103             {
    104                 apple[a]=0;
    105                 Update(fort[a].high,-1);
    106             }
    107             else if(apple[a]==0)
    108             {
    109                 apple[a]=1;
    110                 Update(fort[a].high,1);//high就是对应树状数组中的结点
    111             }
    112         }
    113         else if(ch[0]=='Q')
    114         {
    115             int ans;
    116             ans=getSum(fort[a].high)-getSum(fort[a].low-1);
    117             printf("%d\n",ans);
    118         }
    119     }
    120 
    121     return 0;
    122 }
  • 相关阅读:
    工具类网站收藏
    NodeJS 后端 解决 OPTIONS 请求 404 (Not Found)
    Linux scp 指令
    API及工具类页面链接
    JavaScript正则
    Git 常用
    React组件属性类型(propTypes)
    Meta http-equiv属性详解(转)
    js 刷新页面window.location.reload();
    XHTML标签的嵌套规则分析
  • 原文地址:https://www.cnblogs.com/CKboss/p/3062394.html
Copyright © 2020-2023  润新知