zoukankan      html  css  js  c++  java
  • Apple Tree

    Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 28060   Accepted: 8343

    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
    

    Source

    POJ Monthly--2007.08.05, Huang, Jinsong
    思路:dfs序+树状数组;
    dfs序先将序列映射一下,然后树状数组维护下就可以了复杂度n×log(n);
     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<queue>
     4 #include<stdlib.h>
     5 #include<iostream>
     6 #include<string.h>
     7 #include<set>
     8 #include<map>
     9 #include<vector>
    10 using namespace std;
    11 typedef long long LL;
    12 int ans[100005];
    13 int id[100005];
    14 typedef vector<int> Ve;
    15 vector<Ve>vec(100005);
    16 bool flag[100005];
    17 int cn = 0;
    18 int l[100005];
    19 int r[100005];
    20 void dfs(int n);
    21 int tree[100005];
    22 void update(int x,int n,int c);
    23 int ask(int x);
    24 int main(void)
    25 {
    26     int  n;
    27     memset(flag,0,sizeof(flag));
    28     scanf("%d",&n);
    29     for(int i = 0; i < n-1; i++)
    30     {
    31         int x,y;
    32         scanf("%d %d",&x,&y);
    33         vec[x].push_back(y);
    34         vec[y].push_back(x);
    35     }
    36     dfs(1);
    37     for(int i = 1; i <= n; i++)
    38     {
    39         id[ans[i]] = i;
    40     }
    41     for(int i = 1; i <= n; i++)
    42         update(i,n,1);
    43     int m;
    44     memset(flag,0,sizeof(flag));
    45     scanf("%d",&m);
    46     while(m--)
    47     {
    48         char c[10];
    49         scanf("%s",c);
    50         int x;
    51         scanf("%d",&x);
    52         if(c[0] == 'Q')
    53         {
    54             int ac = ask(r[x]) - ask(l[x]-1);
    55             printf("%d
    ",ac);
    56         }
    57         else
    58         {
    59             if(!flag[x])
    60                 update(id[x],n,-1),flag[x] = true;
    61             else update(id[x],n,1),flag[x] = false;
    62         }
    63     }
    64     return 0;
    65 }
    66 void  dfs(int n)
    67 {
    68     flag[n] = true;
    69     ans[++cn] = n;
    70     l[n] = cn;
    71     for(int i = 0; i < vec[n].size(); i++)
    72     {
    73         int x = vec[n][i];
    74         if(!flag[x])
    75             dfs(x);
    76     }
    77     r[n] = cn;
    78 }
    79 int ask(int x)
    80 {
    81     int sum = 0;
    82     while(x > 0)
    83     {
    84         sum += tree[x];
    85         x -= x&(-x);
    86     }
    87     return sum;
    88 }
    89 void update(int x,int n,int c)
    90 {
    91     while(x <= n)
    92     {
    93         tree[x] +=c;
    94         x += x&(-x);
    95     }
    96 }
  • 相关阅读:
    PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例
    Unity3d + PureMVC框架搭建
    PureMVC--一款多平台MVC框架
    如何在本地搭建IIS服务器
    EasyTouch5初步用法和其中的一个Bug
    kubernetes基础知识点
    CKA认证考试题
    通过接口获取手机号所属城市
    程序员刷题必备
    文件的相对路径详解
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/6412840.html
Copyright © 2011-2022 走看看