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

    Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 23212   Accepted: 7050

    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

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #define N 100010
    using namespace std;
    int n,a[N],tot,lef[N],rig[N],vis[N];
    vector<vector<int> >v(N);              ///最开始用了vector,但是一直不过,百度了某大牛的代码,才发现和自己有同样的问题,改成这样就过了
    int lowbit(int k)
    {
        return k&(-k);
    }
    int getSum(int i)
    {
        int res=0;
        while(i>0)
        {
            res+=a[i];
            i-=lowbit(i);
        }
        return res;
    }
    void add(int i)
    {
        while(i<=n)
        {
            a[i]++;
            i+=lowbit(i);
        }
    }
    void eat(int i)
    {
        while(i<=n)
        {
            a[i]--;
            i+=lowbit(i);
        }
    }
    void dfs(int i)                                 ///深搜对于每一个节点进行编号
    {
        lef[i]=tot;
        for(int j=0;j<v[i].size();j++)
        {
            tot++;
            dfs(v[i][j]);
        }
        rig[i]=tot;
    }
    int main()
    {
        int m;
        while(~scanf("%d",&n))
        {
            memset(vis,0,sizeof(vis));
            memset(lef,0,sizeof(lef));
            memset(rig,0,sizeof(rig));
            memset(a,0,sizeof(a));
            for(int i=0; i<N; i++)
                v[i].clear();
            for(int i=1;i<n;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                v[x].push_back(y);
            }
            tot=1;
            dfs(1);
            for(int i=1;i<=n;i++)
            {
                vis[i]=1;
                add(i);
            }
            for(int i=1;i<=n;i++)
            {
                cout<<lef[i]<<" "<<rig[i]<<endl;
            }
            scanf("%d",&m);
            while(m--)
            {
                char op[5];
                int q;
                scanf("%s%d",op,&q);
                if(!strcmp(op,"Q"))
                    printf("%d
    ",getSum(rig[q])-getSum(lef[q]-1));
                if(!strcmp(op,"C"))
                {
                    if(vis[q])
                        eat(lef[q]);
                    else add(lef[q]);
                    vis[q]=!vis[q];
                }
            }
        }
    }
    


    3
    1 2
    1 3
    3
    Q 1
    C 2
    Q 1
    

    Sample Output

    3
    2



    题目大意:一棵苹果树上有n个苹果,每个叉上都最多只能有1个苹果,1为根节点。现在有2种操作,Q为求以某一节点为根节点的子树上所有苹果的数量和。C是更新某一点上的苹果数量,如果原先为0则长出来一个苹果,否则则被吃掉。

    思路:我们发现本题中的树为一个多叉树,而树的先序遍历即dfs则可以知道以某一节点为根节点的子树所包含的所有节点编号。所以我们用一个left和right数组记录i号苹果在先序遍历下的子树中的最小编号以及最大编号,即它的祖先和自己子树中的所有编号的最大值,然后求和时候用getSum(right[i])-getSum(left[i]-1)即可。


    代码:


  • 相关阅读:
    MVC3 缓存应用
    centos下安装tomcat
    VS2010安装完SP1后再安装Silverlight Tools 4遇到的问题解决办法
    TOMCAT多站点配置
    C#.NET 添加图片水印
    装了vs11后运行MVC3程序出问题
    ASP.NET Session的七点认识
    C# 用正则取文本中所有链接
    Long time no blog...
    从程序员到项目经理(二)
  • 原文地址:https://www.cnblogs.com/Torrance/p/5410564.html
Copyright © 2011-2022 走看看