POJ3321/Apple tree/(DFS序+线段树)
来源:互联网 发布:传奇霸业轮回魔道数据 编辑:程序博客网 时间:2024/05/18 06:19
题目链接
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序+线段树)
- POJ3321/Apple tree/(DFS序+线段树)
- 线段树 poj3321 Apple Tree
- POJ3321 Apple Tree(DFS序 + 树状数组)
- poj3321-Apple Tree-dfs序+树状数组
- Poj3321 Apple Tree【DFS序】【树状数组】
- 【POJ3321】Apple Tree 苹果树 线段树
- 【树状数组--dfs序转化】poj3321 Apple Tree
- (POJ3321)Apple Tree <dfs序列 树状数组>
- POJ3221——Apple Tree(线段树,dfs序)
- POJ 3321 Apple Tree(dfs序+线段树)
- poj3321 Apple Tree(DFS+树状数组)
- 【POJ3321】Apple Tree-DFS+树状数组维护
- poj3321 Apple Tree(树状数组+dfs)
- poj3321-dfs序&树状数组&dfs序模板题-Apple Tree
- poj 3321 Apple Tree(dfs序+线段树)
- POJ 3321 Apple Tree dfs序+线段树
- poj 3321 Apple Tree(dfs序+线段树)
- JZOJ 4679【NOIP2016提高A组8.11】种树
- “让我怎能过大年”参考解法--很暴力
- Android -自定义外卖订单流程
- Android开源项目推荐之「网络请求哪家强」
- 注释转换 续 (C++注释转换为C注释)
- POJ3321/Apple tree/(DFS序+线段树)
- hihocoder #1309 : 任务分配
- EasyUI的自定义验证!
- 拉勾网爬虫
- 51nod (dp)
- UITextField一些常用属性和方法
- LeetCode 135. Candy
- Android开源项目推荐之「图片加载到底哪家强」
- 字符串输入小结