Apple Tree DFS+树状数组

本文介绍了一种针对苹果树的查询与更新算法,通过将树结构转化为一维树状数组,实现对苹果生长和采摘情况的有效跟踪。利用深度优先搜索(DFS)获取节点的时间戳,并采用线段树或树状数组进行区间修改与单点查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 toN 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 forku 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 forkx, 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


首先,这是一棵树,且不是二叉树,然后是区间修改和单点查询,所以要把这棵树转化为一维树状数组,对这棵树进行先序遍历也就是DFS,得到每个结点的管辖范围(我看网上大佬称为时间戳)用数组l和r存储

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#define lowbit(x) (x & (-x))

using namespace std;
const int N = 1e5 + 3;

vector< vector<int> > edge(N);  //直接写vector<int> edge[N],会TLE...
int l[N], r[N], c[N];
bool vis[N];
int n, key = 1;

void DFS(int u)				    //DFS获取时间戳
{
    l[u] = key;
    for(int i = 0; i < edge[u].size(); i++)
    {
        key++;
        DFS(edge[u][i]);
    }
    r[u] = key;
}
void Update(int k, int v)
{
    for(int i = k; i <= n; i += lowbit(i))
        c[i] += v;
}
int GetSum(int k)
{
    int sum = 0;
    for(int i = k; i > 0; i -= lowbit(i))
        sum += c[i];
    return sum;
}
int main()
{
    int u, v, m, x;
    char s[2];
    scanf("%d", &n);
    for(int i = 1; i < n; i++)
    {
        scanf("%d%d", &u, &v);
        edge[u].push_back(v);
    }
    memset(c, 0, sizeof(c));
    memset(vis, 1, sizeof(vis));
    DFS(1);
    for(int i = 1; i <= n; i++)//一开始树上都有苹果
        Update(i, 1);
    scanf("%d", &m);
    while(m--)
    {
        scanf("%s%d", s, &x);
        if(s[0] == 'Q')
        {
            printf("%d\n", GetSum(r[x]) - GetSum(l[x] - 1));
        }
        else
        {
            if(vis[x]) 
                Update(l[x], -1);
            else 
                Update(l[x], 1);
            vis[x] = !vis[x];  //状态相反
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值