• POJ 3321 DFS序


    Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 30636   Accepted: 9162

    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
     
     
    题意:
    给定一颗树,刚开始每个节点上有一个苹果,Q 询问以这个节点为根的子树苹果个数之和。
    C 改变这个结点的苹果树,1则为0,0则改为1;
     
    单点更新,用树状数组即可。复习一下树状数组。
    注意:vector<vector<int> > G(maxn); 邻接表,不然会TLE。
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    
    const int maxn = 100005;
    vector<vector<int> > G(maxn);
    
    int tot;
    int in[maxn];
    int out[maxn];
    
    void dfs(int u,int father) {
        in[u] = ++tot;
        for(int i = 0; i < (int)G[u].size(); i ++) {
            int v = G[u][i];
            if(v==father) continue;
            dfs(v,u);
        }
        out[u] = tot;
    }
    
    int C[maxn];
    int n;
    int lowbit(int x) {
        return  x&-x;
    }
    
    // A[1] + A[2] + ... + A[x]
    int sum(int x) {
        int ret = 0;
        while(x>0) {
            ret +=C[x];
            x-=lowbit(x);
        }
        return ret;
    }
    
    // A[x] +=d
    void add(int x,int d) {
        while(x<=n) {
            C[x] +=d;
            x +=lowbit(x);
        }
    }
    
    int A[maxn];
    
    int main()
    {
        scanf("%d",&n);
    
        for(int i=1; i < n; i++) {
            int u,v;
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
    
        dfs(1,0);
    
        for(int i=1; i <= n; i++)
        {
            A[i] = 1;
            add(i,1);
        }
    
        int m;
        scanf("%d",&m);
        char cmd[20];
        while(m--) {
            scanf("%s",cmd);
            if(cmd[0]=='Q') {
                int x;
                scanf("%d",&x);
                printf("%d
    ",sum(out[x])-sum(in[x]-1));
            }
            else {
                int x;
                scanf("%d",&x);
                if(A[x]==1) {
                    add(in[x],-1);
                    A[x] = 0;
                }
                else {
                    add(in[x],1);
                    A[x] = 1;
                }
            }
        }
    
        return 0;
    }
     
     
  • 相关阅读:
    "Blessing of Dimisionality: High Dimensional Feature and Its Efficient Compression for Face Verification"学习笔记
    java删除目录下符合条件的文件
    java nio buffer读取数据乱码问题
    MySql 存储过程实例(附完整注释)
    mysql truncate、delete与drop区别
    Java的位运算符—与(&)、非(~)、或(|)、异或(^)
    mysql 用户管理和权限设置
    Sublime Text3 安装Markdown
    session 超时设置
    Nginx配置详解
  • 原文地址:https://www.cnblogs.com/TreeDream/p/7442771.html
Copyright © 2020-2023  润新知