当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

6973:Bookshop

题目描述
DreamGrid will go to the bookshop tomorrow. There are $n$ books in the bookshop in total, labeled by $1,2,\dots,n$, connected by $n-1$ bidirectional roads like a tree. Because DreamGrid is very rich, he will buy the books according to the strategy below:
  • Select two books $x$ and $y$.
  • Walk from $x$ to $y$ along the shortest path on the tree, and check each book one by one, including the $x$-th book and the $y$-th book.
  • For each book being checked now, if DreamGrid has enough money (not less than the book price), he'll buy the book and his money will be reduced by the price of the book. In case that his money is less than the price of the book being checked now, he will skip that book.
BaoBao is curious about how rich DreamGrid is. You will be given $q$ queries, in each query you will be given three integers $x_i$, $y_i$ and $z_i$. Assume DreamGrid will bring $z_i$ dollars before buying books, and will walk from $x_i$ to $y_i$, you are asked to tell BaoBao the amount of money DreamGrid will have after he checking all the visited books.
输入解释
The first line contains a single integer $T$ ($1 \leq T \leq 500$), the number of test cases. For each test case:

The first line of the input contains two integers $n$ and $q$ ($1 \leq n,q \leq 100\,000$), denoting the number of books in the bookshop and the number of queries.

The second line contains $n$ integers $p_1,p_2,\dots,p_n$ ($1\leq p_i\leq 10^9$), denoting the price of each book.

Each of the next $n-1$ lines contains two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$, $u_i \neq v_i$), denoting a bidirectional road between $u_i$ and $v_i$. It is guaranteed that the roads form a tree.

In the next $q$ lines, the $i$-th line contains three integers $x_i$, $y_i$ and $z_i$ ($1\leq x_i,y_i\leq n$, $1\leq z_i\leq 10^9$), describing the $i$-th query.

It is guaranteed that the sum of all $n$ is at most $800\,000$, and the sum of all $q$ is at most $800\,000$.
输出解释
For each query, print a single line containing an integer, denoting the amount of money DreamGrid will have after he checking all the visited books.
输入样例
1
5 5
5 2 7 4 6
1 2
1 3
2 4
2 5
4 5 10
5 4 10
4 5 12
5 3 12
3 5 11
输出样例
4
2
0
4
2

该题目是Virtual Judge题目,来自 杭电HDUOJ

源链接: HDU-6973

最后修改于 2021-10-23T19:10:52+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
12000/12000MS(Java/Others) 524288/524288K(Java/Others)