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

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

    这题关键就是用dfs序列 构建树状数组
    这个是多叉树,所以你需要注意L,R

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <queue>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <set>
      7 #include <iostream>
      8 #include <map>
      9 #include <stack>
     10 #include <string>
     11 #include <vector>
     12 #define pi acos(-1.0)
     13 #define eps 1e-6
     14 #define fi first
     15 #define se second
     16 #define lson l,m,rt<<1
     17 #define rson m+1,r,rt<<1|1
     18 #define bug         printf("******
    ")
     19 #define mem(a,b)    memset(a,b,sizeof(a))
     20 #define fuck(x)     cout<<"["<<x<<"]"<<endl
     21 #define f(a)        a*a
     22 #define sf(n)       scanf("%d", &n)
     23 #define sff(a,b)    scanf("%d %d", &a, &b)
     24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
     25 #define pf          printf
     26 #define FRE(i,a,b)  for(i = a; i <= b; i++)
     27 #define FREE(i,a,b) for(i = a; i >= b; i--)
     28 #define FRL(i,a,b)  for(i = a; i < b; i++)
     29 #define FRLL(i,a,b) for(i = a; i > b; i--)
     30 #define FIN freopen("DATA.txt","r",stdin)
     31 #define lowbit(x)   x&-x
     32 #pragma comment (linker,"/STACK:102400000,102400000")
     33 
     34 using namespace std;
     35 const int maxn = 1e5 + 10;
     36 typedef long long LL;
     37 int n, m, lefeid[maxn], rightid[maxn], c[maxn];
     38 int tot, dfscnt, head[maxn], tree[maxn];
     39 struct node {
     40     int v, nxt;
     41 } edge[maxn << 2];
     42 void init() {
     43     tot = 0;
     44     mem(head, -1);
     45 }
     46 void add(int u, int v) {
     47     edge[tot].v = v;
     48     edge[tot].nxt = head[u];
     49     head[u] = tot++;
     50 }
     51 void update(int x, int d) {
     52     while(x <= n) {
     53         c[x] += d;
     54         x += lowbit(x);
     55     }
     56 }
     57 int sum(int x) {
     58     int ret = 0;
     59     while(x > 0) {
     60         ret += c[x];
     61         x -= lowbit(x);
     62     }
     63     return ret;
     64 }
     65 void dfs(int u, int fa) {
     66     lefeid[u] = dfscnt + 1;
     67     for (int i = head[u]; ~i ; i = edge[i].nxt ) {
     68         int v = edge[i].v;
     69         if (v != fa) dfs(v, u);
     70     }
     71     rightid[u] = ++dfscnt;
     72 }
     73 int main() {
     74     while(~scanf("%d", &n)) {
     75         init();
     76         for (int i = 1; i <= n; i++) {
     77             tree[i] = 1;
     78             update(i, 1);
     79         }
     80         for (int i = 1 ; i < n ; i++) {
     81             int u, v;
     82             scanf("%d%d", &u, &v);
     83             add(u, v);
     84             add(v, u);
     85         }
     86         dfscnt = 0;
     87         dfs(1, -1);
     88         scanf("%d", &m);
     89         while(m--) {
     90             char op[10];
     91             int x, l, r;
     92             scanf("%s%d", op, &x) ;
     93             l = lefeid[x], r = rightid[x];
     94             if (op[0] == 'C') {
     95                 if (tree[r]) {
     96                     tree[r] = 0;
     97                     update(r, -1);
     98                 } else {
     99                     tree[r] = 1;
    100                     update(r, 1);
    101                 }
    102             } else printf("%d
    ", sum(r) - sum(l - 1));
    103         }
    104     }
    105     return 0;
    106 }
  • 相关阅读:
    Linux学习32
    Linux学习31-如何查看 linux 系统是centos还是ubuntu,并查看系统版本号
    python测试开发django-124.bootstrap点删除按钮弹确认删除
    python测试开发django-123.bootstrap模态框(modal)垂直居中显示
    python测试开发django-122.bootstrap模态框(modal)学习
    Linux下查询tomcat进程命令
    linux服务器通过X11实现图形化界面显示 1 背景描述
    linux(centos)配置ipv6网卡
    linux 下给网卡添加ipv6、路由
    2021年最新大厂php+go面试题集(1)
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9415345.html
Copyright © 2011-2022 走看看