树的直径

树的直径

Background

(出题者很懒,什么背景也没有留下)

Description

Given a connectivity graph with n nodes and n1 edges, we define the diameter distance of the tree as a pair of point pairs (u,v) on the tree, satisfying the requirement that the shortest distance between u,v is the largest, then the distance is the diameter distance of the tree. Now let's look at node 1 as the root node of the tree. Gaussian has q queries. each time he gives an integer c, satisfying 1≤c≤n, which means after deleting all nodes in the c subtree, he asks what is the diameter distance of the tree at this time. in particular, when c = 1, there is no node in the tree, so the answer is 0 at this time.

给定一个 \(n\) 个节点 \(n−1\) 条边的联通图,我们定义树的直径距离为树上的一对点对 \((u,v)\),满足 \(u,v\) 间的最短距离最大,那么该距离即为树的直径距离。
现在我们将 1 号节点看做树的根节点,高斯有 \(q\) 次询问,每次他会给定一个整数 \(c\),满足\(1≤c≤n\),表示删除 \(c\) 子树中的所有节点后,询问此时树的直径距离为多少,特别地,当 \(c=1\) 时,树上没有任何一个节点,所以此时答案为 0。

Format

Input

The 2 integers in the first row represent n, q.
After n1 rows, given u,v,w v and w in each row, there is an undirected edge of length w between u and v.
After q lines, each line is given an integer c, indicating a query.

第一行2个整数表示 \(n,q\)。
之后 \(n−1\) 行,每行给定 \(u,v,w\) 表示 \(u\) 和 \(v\) 之间有一条长度为 \(w\) 的无向边。
之后 \(q\) 行,每行给定一个整数 \(c\),表示一次询问。

Output

The output has a total of Q lines, and one weight value at a time indicates the answer.

输出共 \(q\) 行,每次输出一个权值表示答案。

Sample 1

Input

3 1
1 2 1
2 3 1
3

Output

1

Limitation

对于60%的数据,\(1≤n,q≤10^3\)。
对于100%的数据,\(1≤n,q≤10^6\),输入数据中所有的数字均为 [0,106]间的整数

Source

Amorphophallus Orz Group

信息

ID
1020
难度
7
分类
树形DP 点击显示
标签
递交数
2
已通过
1
通过率
50%
上传者

相关

在下列训练计划中:

AOG题库训练计划

在下列比赛中:

AOG月赛测试题2「3月月赛Div1」