poj-3321-Apple Tree-(树状数组)

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 30537 Accepted: 9130

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
参考: http://blog.csdn.net/libin56842/article/details/46634095

这里dfs是给节点编号(对应树状数组中的线性编号),一个节点有s,e两个编号,s是他本身的编号,而e是他最大子节点的编号,所以编号完成之后,一个节点包括子树上的所有苹果都可以用sum(e[x])-sum(s[x]-1)来求出,

在dfs(x)时,用一个全局变量(tot)给所有点编号,大的是祖先,小的是子孙,首先对于dfs(x)当前tot就是他的s[x],然后dfs他的每个子节点,当所有子节点dfs完成,那么他的所有子树下标已经确定,这是tot就是他的最大子节点编号e[x]

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
#define M 100005
int n,m,tot;
int tree[M],s[M],e[M];
bool vis[M];
vector< vector<int> >a(M);
int lowbit(int i)
{
    return i&(-i);
}
void add(int i,int v)
{
    while(i<=n)
    {
        tree[i]+=v;
        i+=lowbit(i);
    }
}
int sum(int i)  //一定注意i>=1,切记i不能为0
{
    int res=0;
    while(i>0)
    {
        res+=tree[i];
        i-=lowbit(i);
    }
    return res;
}
void dfs(int x)
{
    s[x]=tot;
    for(int i=0;i<a[x].size();i++)
    {
        tot++;
        dfs(a[x][i]);
    }
    e[x]=tot;
}
int main()
{
    int i,x,y;
    char op[5];
    while(scanf("%d",&n)!=EOF)
    {
        memset(tree,0,sizeof(tree));
        memset(vis,false,sizeof(vis));
        memset(s,0,sizeof(s));
        memset(e,0,sizeof(e));
        for(i=0;i<M;i++)
            a[i].clear();
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            a[x].push_back(y);
        }
        tot=1;
        dfs(1);  //1是根节点
        for(i=1;i<=n;i++)
        {
            vis[i]=true;
            add(i,1);
        }
        scanf("%d",&m);
        for(i=1;i<=m;i++)
        {
            scanf("%s%d",op,&x);
            if(op[0]=='Q')
            {
                printf("%d\n",sum(e[x])-sum(s[x]-1));
            }
            else
            {
                if(vis[x])
                {
                    add(s[x],-1);
                    vis[x]=false;
                }
                else
                {
                    add(s[x],1);
                    vis[x]=true;
                }
            }
        }
    }
    return 0;
}

还可以用边表存树,

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 100007
using namespace std;

int s[maxn],e[maxn];
int a[maxn];
int num;
bool vt[maxn];
struct node
{
    int v;
    int next;
}g[maxn];

int head[maxn],cnt,n;

void init()
{
    int i;
    memset(head,-1,sizeof(head));
    cnt = 0;
    for (i = 0; i <= n; ++i)
    {
        a[i] = 0; vt[i] = false;
    }
    num = 0;
}
void add(int u,int v)
{
    g[cnt].v = v;
    g[cnt].next = head[u];
    head[u] = cnt++;
}
void dfs(int pos)
{
    s[pos] = ++num;
    for (int i = head[pos]; i != -1; i = g[i].next)
    {
        int v = g[i].v;
        dfs(v);
    }
    e[pos] = num;
}

int lowbit(int x)
{
    return x&(-x);
}

void modify(int pos,int sc)
{
    while (pos <= n)
    {
        a[pos] += sc;
        pos += lowbit(pos);
    }
}
int getsum(int pos)
{
    int sum = 0;
    while (pos > 0)
    {
        sum += a[pos];
        pos -= lowbit(pos);
    }
    return sum;
}
int main()
{
    //freopen("d.txt","r",stdin);
    int x,y,i;
    int q;
    scanf("%d",&n);
    init();
    //建树
    for (i = 0; i < n -1; ++i)
    {
        scanf("%d%d",&x,&y);
        add(x,y);
    }
    //dfs重新编号
    dfs(1);
    //树状数组典型操作
    for (i = 1; i <= n; ++i)
    modify(i,1);
    char op[3];
    scanf("%d",&q);
    while (q--)
    {
        scanf("%s%d",op,&x);
        if (op[0] == 'Q')
        {
            printf("%d\n",getsum(e[x]) - getsum(s[x] - 1));
        }
        else
        {
            if (!vt[x])
            {
                modify(s[x],-1);
                vt[x] = true;
            }
            else
            {
                modify(s[x],1);
                vt[x] = false;
            }
        }
    }
    return 0;



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值