题目链接:http://poj.org/problem?id=3321

Apple Tree

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 34812

Accepted: 10469

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

Source

POJ Monthly--2007.08.05, Huang, Jinsong

题意概括:

给一个 N 阶无向图,要求把这个无向图转换为一颗以 1 为根的树后进行两种操作:

C x :x结点上如果有苹果就摘掉,如果没有苹果就长出一个新的来

Q x:   查询 x 结点与它的所有后代分支一共有几个苹果

解题思路:

因为原本苹果之间的关系我们不好处理,所以就给他们这些结点重新编号。

并且为了方便子树的苹果数求和(即区间查询)我们在给结点增加一个左值一个右值表示当前结点管辖的范围。

如何建树?

根据题目一开始给的边与边的关系我们可以通过 dfs 进行建树。

L[ x ] (X的左值): X结点的新编号就是该结点的左值,表示它管辖范围的下限

R[ x ] (X的右值):而子树的编号最大值为该结点的右值,表示它管辖范围的上限。

至于dfs顺序,如下

举个栗子:

N = 6;

1 - 2

1 - 6

2 - 5

2 - 3

6 - 4

到这里感觉有一点点线段树的味道了。。。

但这里要用树状数组对这些区间进行维护和查询。

例如:我要 Q 2;

由上面建好的新树我们可以知道 结点2这颗子树包含了结点2、结点5、结点3,而结点2所管辖的区间【2,4】刚好把它们包括进去了。

如果我们要求 这可子树的苹果数量,其实就是用树状数组求区间【2,4】的苹果总数

也就是右值的前缀和减去左值的前缀和(但不包括左值,因为结点本身也要算进去)即:ans = sum( R[ 2 ] ) - sum( L[ 2 ] - 1);

而C操作其实就是树状数组的单点更新而已。

注意事项:

一开始用 stl 的 vector 存无向图,虽然过了但效率不咋地。想想好久没用静态邻接表了,换了一下,快了好多。

AC code:

 ///建树+树状数组(stl)8080k 1047ms
/*
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef vector<int>ve; const int MAXN = 1e5+10;
int t[MAXN], l[MAXN], r[MAXN];
bool vis[MAXN];
int N, M, cnt; vector<ve>node(MAXN); int lowbit(int x)
{
return x&(-x);
}
void add(int x, int value)
{
for(int i = x; i <= N; i+=lowbit(i))
t[i]+=value;
}
int sum(int x)
{
int res = 0;
for(int i = x; i > 0; i-=lowbit(i))
res+=t[i];
return res;
}
void dfs(int k)
{
l[k] = cnt;
for(int i = 0; i < node[k].size(); i++)
{
cnt++;
dfs(node[k][i]);
}
r[k] = cnt;
return;
}
int main()
{
scanf("%d", &N);
int a, b;
for(int i = 1; i <= N; i++)
{
vis[i] = 1;
add(i, 1);
}
for(int i = 1; i < N; i++)
{
scanf("%d%d", &a, &b);
node[a].push_back(b);
}
cnt = 1;
dfs(1);
scanf("%d", &M);
while(M--)
{
char com[3];
int px;
scanf("%s", &com);
if(com[0] == 'C')
{
scanf("%d", &px);
if(vis[px]) add(l[px], -1);
else add(l[px], 1);
vis[px] = !vis[px];
}
else
{
scanf("%d", &px);
int ans = sum(r[px]) - sum(l[px]-1);
printf("%d\n", ans);
}
}
return 0;
}
*/ ///建树+树状数组(静态连接表) 4940k 454ms
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e5+;
const int MAXM = 1e5+; struct node
{
int to, next;
}edge[MAXM<<]; int head[MAXN];
bool vis[MAXN];
int t[MAXN], l[MAXN], r[MAXN];
int N, M, cnt, key; int lowbit(int x)
{
return x&(-x);
}
void add(int x, int value)
{
for(int i = x; i <= N; i+=lowbit(i))
t[i]+=value;
}
int sum(int x)
{
int res = ;
for(int i = x; i > ; i-=lowbit(i))
res+=t[i];
return res;
} void dfs(int k)
{
l[k] = key;
for(int i = head[k]; i != -; i = edge[i].next)
{
if(l[edge[i].to]) continue;
key++;
dfs(edge[i].to);
}
r[k] = key;
} void init()
{
memset(head, -, sizeof(head));
cnt = ;
} void add_e(int u, int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
} int main()
{
scanf("%d", &N);
init();
int a, b;
for(int i = ; i <= N; i++)
{
vis[i] = ; //每个节点一开始都有苹果
add(i, ); //初始化树状数组
}
for(int i = ; i < N; i++)
{
scanf("%d%d", &a, &b); ///建无向图
add_e(a, b);
add_e(b, a);
}
key = ; dfs(); //建树
scanf("%d", &M);
int px;
char com[];
while(M--)
{
scanf("%s", &com);
if(com[] == 'C')
{
scanf("%d", &px);
if(vis[px]) add(l[px], -);
else add(l[px], );
vis[px] = !vis[px];
}
else
{
scanf("%d", &px);
printf("%d\n", sum(r[px])-sum(l[px]-));
}
}
return ;
}

POJ 3321 Apple Tree 【树状数组+建树】的更多相关文章

  1. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  2. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  3. POJ 3321 Apple Tree 树状数组+DFS

    题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...

  4. POJ 3321 Apple Tree 树状数组 第一题

    第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...

  5. 3321 Apple Tree 树状数组

    LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...

  6. POJ 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...

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

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

  8. E - Apple Tree(树状数组+DFS序)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  9. POJ3321 Apple Tree(树状数组)

    先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...

  10. POJ 2486 Apple Tree [树状DP]

    题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...

随机推荐

  1. PIXI 宝物猎人(7)

    介绍 ,本实例来自官网 代码结构 打开 treasureHunter.html 文件,你将会看到所有的代码都在一个大的文件里.下面是一个关于如何组织所有代码的概览: //Setup Pixi and ...

  2. 有关tensorflow一些问题

    1.python版本 采用64位的python 2.系统不支持高版本tensorflow(>1.6),运行报错如下: 问题描述如下: ImportError: DLL load failed: ...

  3. 自动化交互expect

    自动化交互expect 一,介绍 每次服务器控制链接都需要输入密码,很麻烦,每次交互大大延长了时间 因此就有了免交互及自动化交互存在expect 二,安装 yum install expect -y ...

  4. vim创建新的命令

    转自:http://man.chinaunix.net/newsoft/vi/doc/usr_5F40.html#usr_40.txt *40.1* 键映射 简单的映射已经在 |05.3| 介绍过了. ...

  5. 很有用的PHP笔试题系列三

    1. 什么事面向对象?主要特征是什么? 面向对象是程序的一种设计方式,它利于提高程序的重用性,使程序结构更加清晰.主要特征:封装.继承.多态. 2. SESSION 与 COOKIE的区别是什么,请从 ...

  6. [转] EF cannot be tracked because another instance of this type with the same key is already being tracked

    本文转自:http://stackoverflow.com/questions/6033638/an-object-with-the-same-key-already-exists-in-the-ob ...

  7. vsCode代码缩略图

    vsCode配置代码缩略图: 文件--首选项--设置 搜索 minimap    true 打开 false 关闭

  8. [转]最全Redis面试题整理

    此为转载文章,仅做记录使用,方便日后查看,原文链接:http://www.bieryun.com/3405.html 1.什么是Redis? 答:Redis全称为:Remote Dictionary ...

  9. git 创建远程版本库(亲测有效)

    一.github远程版本库 1.创建SSH Key(windows)   ssh-keygen -t rsa -C "youremail@example.com"   2.连接版本 ...

  10. grunt 常用插件有哪些?

    作者:顾城链接:https://www.zhihu.com/question/21917526/answer/19747259来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...