专栏首页算法修养POJ--3321 Apple Tree(树状数组+dfs(序列))

POJ--3321 Apple Tree(树状数组+dfs(序列))

Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 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

题意: 就是给你一棵树,每一个结点一开始都有一个苹果,然后有两个操作,一个是改变一个结点的值。即有苹果变无苹果,无苹果变有苹果。另一个是求一个结点下面的子树有多少个苹果。这个题目,询问次数100000,显然要用树状数组。属于更新点,求区间和。要知道树状数组无非两种类型,更新点求区间,更新区间求点。此外还要会dfs序列。具体见代码里dfs()函数。dfs序列可以自己百度一下是什么。这道题目是最基础的在树的结构里用树状数组统计

#include <iostream>
#include <string.h>
#include <math.h>

using namespace std;

#define MAX 100010
int head[MAX];
int c[MAX];
int apple[MAX];
int tol;
int n,m;
int tag;
int _right[MAX];
int _left[MAX];
int ans;
int vis[MAX];
struct Node
{
    int value;
    int next;
}edge[MAX*2];
void modify(int x,int y)
{
    edge[tol].value=y;
    edge[tol].next=head[x];
    head[x]=tol++;
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int num)
{
    while(x<=n)
    {
        c[x]+=num;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int _sum=0;
    while(x>0)
    {
        _sum+=c[x];
        x-=lowbit(x);
    }
    return _sum;
}
void dfs(int x)
{
    vis[x]=1;
    tag++;
    _left[x]=tag;
    int a=head[x];
    while(a!=-1)
    {
      if(vis[edge[a].value]==0)

        dfs(edge[a].value);

        a=edge[a].next;
    }
    _right[x]=tag;
}
void init()
{
    memset(c,0,sizeof(c));
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
}
int main()
{
    int x,y;
    char a;
    int b;
    scanf("%d",&n);
    tol=0;
    tag=0;
    init();
    for(int i=1;i<=n;i++)
    {
        update(i,1);
        apple[i]=1;
    }
       for(int i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        modify(x,y);
        modify(y,x);
    }
    dfs(1);
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        getchar();
        scanf("%c",&a);
        scanf("%d",&b);
        if(a=='C')
        {
            if(apple[b]==1)
            {
                update(_left[b],-1);
                apple[b]=0;
            }
            else
            {
                update(_left[b],1);
                apple[b]=1;
            }
        }
        else
        {
            ans=sum(_right[b])-sum(_left[b]-1);
            printf("%d\n",ans);
        }


    }
    return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • LeetCode Contest 181

    遍历除数的时候从1到sqrt(nums[i]) 10000*sqrt(100000) 是不会超时的

    ShenduCC
  • HDU 3450 Counting Sequences(线段树)

    Counting Sequences Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 3276...

    ShenduCC
  • HDU 5157 Harry and magic string(回文树)

    Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

    ShenduCC
  • LWC 51:683. K Empty Slots

    LWC 51:683. K Empty Slots 传送门:683. K Empty Slots Problem: There is a garden wit...

    用户1147447
  • poj-3660-cows contest(不懂待定)

    N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a program...

    瑾诺学长
  • 九度OJ——1107搬水果

    题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果合成一堆。每一次合并,小明可以把两堆水果...

    AI那点小事
  • ZOJ 3631 Watashi's BG(01dp)

    01dp不过由于数组过于大,开不开,学了搜索过了,先记录下 还有一种方法 #include<stdio.h> #include<algorithm> using...

    用户1624346
  • Leetcode 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth larg...

    triplebee
  • 04-树6. Huffman Codes--优先队列(堆)在哈夫曼树与哈夫曼编码上的应用

    题目来源:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%916 In 1953, David A. Huffm...

    llhthinker
  • C++快速补天

    (2)int型范围:-2^31~(2^31)-1,大致范围在-2*10^9~2*10^9(占用32bit/4Byte)

    lollipop72

扫码关注云+社区

领取腾讯云代金券