POJ 3321 Apple Tree(DFS + 树状数组)

Apple Tree

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

原文链接:POJ 3321 Apple Tree (树状数组)

题目大意:给出一棵树,最初每个节点上都有一个苹果。有两种操作,修改(即修改某一个节点,修改时这一个节点苹果从有到无,或从无到有)和查询(查询某一个节点他的子树上有多少个苹果)。

解题思路:由于此题数据比较大(N<=10^5),而且不是标准的二叉树,所以这里我们队每一个节点重新编号,另外为每一个节点赋一个左值和一个右值,表示这个节点的管辖范围。


上图也就是DFS搜索的时候做标记的过程,这样新的编号为1~6的节点所管辖的范围分别就是[1,6]  [2,4]   [3,3]  [4,4]  [5,6]  [6,6],其中左边的是左值,右边的是右值,节点1的区间是[1,6],正好这棵子树有6个节点,其他的情况也一样。


我们求出每一个节点从1~左值的和1~右值的和,他们的差就是这个节点的子树的所有的和(即这棵子树苹果数目)
最后每输入一组数据就进行依次操作就可以了

代码如下:

#include <cstdio>
#include <cstring>
#include <vector>
#define MAXN 100005
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;

int TreeArray[MAXN], Left[MAXN], Right[MAXN], Fork[MAXN];
typedef vector<int> Ve;
vector<Ve>Edge(MAXN);
int N,M;
int key;

void init()//初始化数组和
{
    mem(Left);  mem(Right);
    mem(Fork);  mem(TreeArray);
    for(int i=0;i<MAXN;i++)Edge[i].clear();
}

void DFS(int node)//为每一个node添加一个左值和右值,表示这个节点所
{
    Left[node] = key;
    for(int i=0;i<Edge[node].size();i++)
    {
        key+=1;
        DFS(Edge[node][i]);
    }
    Right[node] = key;
}

int LowBit(int x)//返回的是2^k
{
    return x & (x ^ (x-1));
}

void Edit(int k, int num)//修改节点k,如果是添加一个,代入1,删除一个代入-1
{
    while(k <= N)
    {
        TreeArray[k] += num;
        k += LowBit(k);
    }
}

int GetSum(int k)//得到1...k的和
{
    int sum = 0;
    while(k>=1)
    {
        sum += TreeArray[k];
        k -= LowBit(k);
    }
    return sum;
}

void ReadDataAndDo()
{
    int a,b;
    char ch;
    for(int i=1;i<N;i++)//输入a,b把边存放在容器里面
    {
        scanf("%d%d", &a, &b);
        Edge[a].push_back(b);
    }
    key = 1;    DFS(1);//为每一个节点对应一个左边界和右边界,他自己就存放在左边界里面,而它的管辖范围就是左边界到右边界
    for(int i=1;i<=N;i++)
    {
        Fork[i] = 1;//最初每个Fork上都有一个苹果
        Edit(i,1);//同时更新树状数组的值
    }
    scanf("%d%*c", &M);
    for(int i=0;i<M;i++)
    {
        scanf("%c %d%*c", &ch, &b);
        if(ch == 'Q')//b的子树就是[Left[b], right[b]]
        {
            printf("%d\n", GetSum(Right[b]) - GetSum(Left[b]-1));
        }
        else
        {
            if(Fork[b]) Edit(Left[b],-1);//由于每个节点的编号就是它的左值,所以直接修改左节点
            else Edit(Left[b],1);
            Fork[b] = !Fork[b];//变为相反的状态
        }
    }
}

int main()
{
    while(~scanf("%d", &N))
    {
        init();
        ReadDataAndDo();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值