Marge is already preparing for Christmas and bought a beautiful tree, decorated with shiny ornaments. Her Christmas tree can be represented as a complete binary tree composed of N nodes, numbered from 1to N and rooted on node 1. Each node has an integer value associated to it, representing its shininess.

The shininess of the h - th level of the tree is the sum of the shininess of all the nodes with depth h and the shininess of the tree is the largest value of shininess of its levels.

Nicoleta has a crush on a girl and wants to give her a part of Marge's beautiful tree. To do so, he will choose a node u and give his crush the subtree rooted at node u, including u. However, he doesn't want to get in (too much) trouble with Marge, so he will consider some candidates before making the cut.

Nicoleta has M candidate nodes to be the root of the cut subtree. For each candidate, Nicoleta wants to know what is the value of shininess of the remaining tree.

Input

The first line of the input contains a three integers N (2 ≤ N ≤ 105) and M (1 ≤ M ≤ 105) and w (0 ≤ w ≤ 104), indicating, respectively, the number of nodes of the tree, the number of candidate nodes and the shininess of node 1.

Each of the next N - 1 lines contains three integers u (2 ≤ u ≤ N) , v (1 ≤ v ≤ N) and w (0 ≤ w ≤ 104), indicating that node u is a child of node v and has shininess w.

M lines follow, each with a single integer u (2 ≤ u ≤ N), indicating the number of a candidate node.

Output

For each candidate node, in the order that they appear in the input, output a single line containing a single integer: the shininess of the remaining tree.

Example

Input
6 2 3
4 1 1
5 1 4
2 4 7
3 4 6
6 5 5
4
5
Output
5
13

Note

More about complete binary trees: https://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees

题解:题目给出一个完全二叉树,并定义一个此树中每层的权值为该层的每个点的权值之和,总的权为每层中最大的那个.

让我们求去掉以u为根的子树后,所得的剩下的树的权(shinness). 由于是完全二叉树,则每个点的儿子的下标为id*2,id*2+1,并且每一层的下标范围为[2^i,2^(i+1)-1](第i层,从0开始计数).那么去掉一颗子树后,损失的信息就可以区间求和来快速算出来, 总共的该层的权重之和也可以快速算出来.

用线段树,树状数组,ST表什么的都可以哇~~.

首先建立完全二叉树根据预处理得到的每个点的siz[i]数组大小(以i为根的子树元素有多少个).建立正确的完全二叉树,然后为其建立新的下标,对应下标赋予正确的权值.然后对于每一个询问u,我们首先找根节点1,看去掉u这一层能否影响到1这一层,然后向下递归就好啦.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
int val[maxn],dfn[maxn];
int siz[maxn],data[maxn];
int sum[maxn<<];
vector<int>G[maxn];
void dfs(int u)
{
siz[u]=;
for(int i=;i<G[u].size();i++){
dfs(G[u][i]);
siz[u]+=siz[G[u][i]];
}
} void DFS(int u,int index)
{
dfn[u]=index;
if(G[u].size()==)return ;
else if(G[u].size()==){
DFS(G[u][],index*);
}
else{
if(siz[G[u][]]>=siz[G[u][]]){
DFS(G[u][],index*);
DFS(G[u][],index*+);
}
else{
DFS(G[u][],index*+);
DFS(G[u][],index*);
}
}
}
void build(int l,int r,int rt)
{
if(l==r){
sum[rt]=data[l];
return ;
}
int mid=(l+r)/;
build(l,mid,rt*);
build(mid+,r,rt*+);
sum[rt]=sum[rt*]+sum[rt*+];
}
int querysum(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)return sum[rt];
int ans=;
int mid=(l+r)/;
if(L<=mid)ans+=querysum(L,R,l,mid,rt*);
if(R>=mid+)ans+=querysum(L,R,mid+,r,rt*+);
return ans;
}
int main()
{
int n,m,w;
cin >> n >> m >> w;
val[]=w;
for(int i = ; i <= n-; i++){
int u, v, w;
cin >> u >> v >> w;
G[v].push_back(u);
val[u]=w;
} dfs();
DFS(,);
for(int i = ; i <= n; i++){
data[dfn[i]]=val[i];
}
build(,n,);
while(m--){
int u,ans=;
cin >> u;
u=dfn[u];
int now1=,siz1=;
int now2=u,siz2=;
while(now1<=n){
if(now2>=now1&&now2<=min(n,now1+siz1-)){
ans=max(ans,querysum(now1,min(n,now1+siz1-),,n,)-querysum(now2,min(n,now2+siz2-),,n,));
now2*=;
siz2*=;
}
else{
ans=max(ans,querysum(now1,min(n,now1+siz1-),,n,));
}
now1*=;
siz1*=;
}
cout<<ans<<endl;
}
return ;
}

F - No Link, Cut Tree! Gym - 101484F的更多相关文章

  1. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  2. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  3. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

  4. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  5. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  6. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  7. AC日记——【模板】Link Cut Tree 洛谷 P3690

    [模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...

  8. 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

    一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...

  9. 学习笔记:Link Cut Tree

    模板题 原理 类似树链剖分对重儿子/长儿子剖分,Link Cut Tree 也做的是类似的链剖分. 每个节点选出 \(0 / 1\) 个儿子作为实儿子,剩下是虚儿子.对应的边是实边/虚边,虚实时可以进 ...

随机推荐

  1. SpringAOP 使用注解的简单使用

    1. 导入jar包 /SpringAOPmy/lib/com.springsource.net.sf.cglib-2.2.0.jar/SpringAOPmy/lib/com.springsource. ...

  2. bugku-Web flag.php

    打开网页发现并没有什么,试了很多次没用. 其实题目中提示了hint,我们就传递一个hint=1试试,发现出现了代码: <?php error_reporting(0); include_once ...

  3. selumium 中 xpath获取文本、属性正确写法

    报错“The result of the xpath expression is: [object Attr]. It should be an element” yutube爬虫动态加载,需要用到s ...

  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于XML装配Bean

    Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...

  5. SAP_FICO常用事务代码

    1.会计科目维护: FS00:总账科目主记录维护 FSP0:科目表中总账科目主记录维护 FSS0:公司代码中总账科目主记录维护 2.会计凭证创建 FB01:创建凭证 F-02:总账凭证创建(在FB01 ...

  6. Chrome在新版MacOS上报错 NET::ERR_CERT_WEAK_KEY 解决方法

    现象 原文链接 证书详情: 原因 参考苹果官网给出的提示(https://support.apple.com/en-us/HT210176): RSA秘钥长度必须>=2048,小于这个长度的将不 ...

  7. rabbitmq安装及简单demo练习

    参考:https://my.oschina.net/loveorange/blog/3026473 安装参考链接: 1. 下载自己需要的rabbitmq_server(http://www.rabbi ...

  8. Linux下yum出现no module named pycurl 解决办法

    1.1 no module named pycurl 解决办法 下载curl:http://curl.haxx.se/download/curl-7.21.3.tar.gz .tar.gz ./con ...

  9. PPT |《Kubernetes的兴起》

    京东云开发者社区技术沙龙--<Cloud Native时代的应用之路与开源创新> Part1-<Kubernetes的兴起> 欢迎点击"链接"了解更多精彩内 ...

  10. windows 10下的python开发环境

    linux子系统 按照文档 https://www.jianshu.com/p/2bcf5eca5fbc 的前五步,完成 ubuntu子系统安装. 不需安装图形桌面,无使用价值. 在https://w ...