题目链接

Apple Tree

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 9692 Accepted: 3217

Description

Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn’t allow Wshxzt to go more than K steps in the tree. It costs one step when she goes from one node to another adjacent node. Wshxzt likes apple very much. So she wants to eat as many as she can. Can you tell how many apples she can eat in at most K steps.

Input

There are several test cases in the input

Each test case contains three parts.

The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 … N. Since it is a tree, each node can reach any other in only one route. (1<=N<=100, 0<=K<=200)

The second part contains N integers (All integers are nonnegative and not bigger than 1000). The ith number is the amount of apples in Node i.

The third part contains N-1 line. There are two numbers A,B in each line, meaning that Node A and Node B are adjacent.

Input will be ended by the end of file.

Note: Wshxzt starts at Node 1.

Output

For each test case, output the maximal numbers of apples Wshxzt can eat at a line.

Sample Input

2 1

0 11

1 2

3 2

0 1 2

1 2

1 3

Sample Output

11

2

Source

POJ Contest,Author:magicpig@ZSU

/*
DFS序到线段树是一种映射关系,基础DFS序问题
*/
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e6+5;
struct edge
{
int id;
int next;
};
edge E[maxn<<1];
int head[maxn];
int ecnt;
void add(int u,int v)
{
E[ecnt]={v,head[u]};
head[u]=ecnt++;
}
int vis[maxn];
int l[maxn],r[maxn];
int cnt;
void DFS(int u)
{
vis[u]=1;
l[u]=++cnt;
for(int i=head[u];~i;i=E[i].next)
{
if(!vis[E[i].id])
{
DFS(E[i].id);
}
}
r[u]=cnt;
}
struct node
{
int l;
int r;
int sum;
};
node tree[maxn<<2];
void build(int id,int l,int r)
{
tree[id].l=l;
tree[id].r=r;
tree[id].sum=r-l+1;
if(l==r)
return ;
int mid=(l+r)>>1;
build(id<<1 , l, mid);
build(id<<1|1, mid+1, r);
}
void pushup(int id)
{
tree[id].sum=tree[id<<1].sum+tree[id<<1|1].sum;
}
void update(int id ,int pos)
{
if(tree[id].l==tree[id].r)
{
tree[id].sum^=1;
}
else
{
int mid=(tree[id].l+tree[id].r)>>1;
if(pos>mid)
update(id<<1|1, pos);
else
update(id<<1, pos);
pushup(id);
}
}
int query(int id,int l,int r)
{
if(tree[id].l==l&&tree[id].r==r)
{
return tree[id].sum;
}
int mid=(tree[id].l+tree[id].r)>>1;
if(r<=mid)
return query(id<<1, l, r);
else if(l>mid)
return query(id<<1|1, l, r);
else
{
int ans=0;
ans+=query(id<<1, l, mid);
ans+=query(id<<1|1, mid+1, r);
return ans;
}
}
void init()
{
memset(head, -1, sizeof(head));
ecnt=0;
cnt=0;
}
int n,m;
int main ()
{
scanf("%d",&n);
int u,v;
init();
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
add(u, v);
add(v, u);
}
DFS(1);
build(1, l[1], r[1]);
scanf("%d",&m);
int id;
char s[10];
for(int i=0;i<m;i++)
{
scanf("%s%d",s,&id);
if(s[0]=='Q')
{
int ans=query(1, l[id], r[id]);
printf("%d\n",ans);
}
else
{
update(1, l[id]);
}
}
return 0;
}

POJ3321/Apple tree/(DFS序+线段树)的更多相关文章

  1. POJ3321 - Apple Tree DFS序 + 线段树或树状数组

    Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...

  2. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  3. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  4. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  5. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. codechef T6 Pishty and tree dfs序+线段树

    PSHTTR: Pishty 和城堡题目描述 Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格 的古堡和非常聪明的熊闻名全国. 胡斯特的镇城之宝是就是这么一座古 ...

  7. codeforces 620E. New Year Tree dfs序+线段树+bitset

    题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...

  8. CodeForces 620E:New Year Tree(dfs序+线段树)

    E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...

  9. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  10. 【cf343】D. Water Tree(dfs序+线段树)

    传送门 题意: 给出一个以\(1\)为根的有根树,起始每个结点都为\(0\),现在有三种操作: 1.将\(v\)及\(v\)的子树都置为\(1\): 2.将\(v\)及其所有的祖先都置为\(0\): ...

随机推荐

  1. C/C++ - <string> 与<string.h>、<cstring>的区别

    <string.h><string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <string><string>是C ...

  2. boost.spirit之解析C++头文件

    环境:win7_64旗舰版,VS2008 场景:C++与lua交互是比较繁琐的,当我们编写一个C++类后,如果要给lua使用,就必须写一个lua包装类,将lua与原始C++类关联起来.其实这部分代码编 ...

  3. grub4dos新手指南-1

    对于多数没有或很少接触过linux的windows用户来说,刚开始使用grub时离不开菜单.也就是说,使用grub前,我们要准备menu.lst文件.先讲一些最为常用的命令help用来显示其它命令的用 ...

  4. Redis简介四

    一..NET使用Redis 和MongoDB一样,在.NET中使用Redis其实也是使用第三方驱动,官网推荐的是使用ServiceStack.Redis(https://github.com/Serv ...

  5. RPC框架基本原理(二):客户端注册

    客户端的注册流程如下 核心功能主要如下: 1.生成调用远程HSF服务的代理 此代理的效果为生成ServiceMetadata中指定的interface的代理,调用时可将代理转型为服务接口,并进行直接的 ...

  6. linux时间同步,ntpd、ntpdate

    linux时间同步,ntpd.ntpdate 在Windwos中,系统时间的设置很简单,界面操作,通俗易懂.而且设置后,重启,关机都没关系.系统时间会自动保存在Bios的时钟里面,启动计算机的时候,系 ...

  7. NGUI实现一个背包功能

    界面布局是这样的,一个400*400的背景,然后在其上是16张小图片,每个小图片格子可以用来放置拾取的物品.有两个预制体,一个是可放置的小格子,一个是拾取的物品(包含一个此物品有多少的Label). ...

  8. Chapter 1 First Sight——28

    "Which one is the boy with the reddish brown hair?" 那个红褐色头发的男孩是谁? I asked. I peeked at him ...

  9. Web开发人员不要错过的60款用户界面设计工具(上)

    Web开发大师们,干货再次来袭!小编为大家盘点了60款功能丰富类型各异的用户界面设计工具,本系列将以上中下三篇分别为大家呈现.今天盘点的这20款工具囊括了大量界面原型设计工具,有免费的在线原型工具,有 ...

  10. hdu_2871_Memory Control(巨恶心线段树)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2871 题意:给你一段内存,让你操作1:Reset:重置所有内存 2:New x:申请一块X大小的内存, ...