题目链接: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. GreenPlum 大数据平台--非并行备份(六)

    一,非并行备份(pg_dump) 1) GP依然支持常规的PostgreSQL备份命令pg_dump和pg_dumpall 2) 备份将在Master主机上创建一个包含所有Segment数据的大的备份 ...

  2. 遍历方式 && 数组方法 && 算法

    遍历方式 一般,我们常用for in遍历对象,使用for (var i = 0; i < len; i++) {}的方式来遍历数组,这是最常用的两种方式,但是优缺点呢? 1.for (var i ...

  3. laravel框架的rabbitmq使用示例[多队列封装]

    RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的.所有主要 ...

  4. Mysql系列-字符集

    字符集 怎样选择合适的字符集 如果应用程序需要发布到很多国家和地区,需要支持各种各样的文字,则选择Unicode编码,Mysql中即UTF-8.q如果需要将数据导入数据库,这时候要注意数据库字符集对数 ...

  5. mysql 数据库8.0版本,jdbc驱动连接问题

    前言 8.0版本的mysql数据的连接 与 5.0的有所不同,下面直接贴出  8.0版本应该有的 jdbc驱动连接,还有 mysql 的jdbc jar包要8.0以上的 内容如下 : jdbc.dri ...

  6. python制作 whl 源文件,并制作本地pip源

    制作whl 1.创建用于存放wheel文件目录 mkdir wheels 2.安装wheel库 pip install  wheel 3.进入wheels目录 cd wheels 4.使用pip wh ...

  7. hello2详解

    1.GreetingServlet.java(显示问候页面表单) 此servlet重写该doGet方法,实现GETHTTP方法.servlet显示一个简单的HTML问候表单,其提交按钮就像hello1 ...

  8. CSS设计模式之三权分立模式篇 ( 转)

    转自 海玉的博客 市面上我们常常会看到各种各样的设计模式书籍,Java设计模式.C#设计模式.Ruby设计模式等等.在众多的语言设计模式中我唯独找不到关于CSS设计模式的资料,即使在网上找到类似内容, ...

  9. Android基础Activity篇——创建一个活动(Activity)

    1.创建活动 首先用AS创建一个add no activity项目名使用ActivityTest,包名为默认的com.example.activitytest 2.右击app.java.com.exa ...

  10. 【Linux】GDB程序调试

    一.GDB简介 GDB是GNU发布的一款功能强大的程序调试工具.GDB主要完成下面三个方面的功能: 启动被调试程序. 让被调试的程序在指定的位置停住. 当程序被停住时,可以检查程序状态(如变量值) 二 ...