#P3242. [HNOI2015] 接水果

    ID: 2291 远端评测题 6000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2015湖南深度优先搜索,DFS最近公共祖先,LCA树套树

[HNOI2015] 接水果

Description

Kazami Yuuka really enjoys playing a game called osu!, and her favorite mode is catching fruits. Since she has already DT FC’d The Big Black, she thinks the game is too easy, so she invented a harder version.

First, there is a map that is a tree with nn vertices and n1n-1 edges.

There are pp plates on this tree. Each plate is actually a path, and each plate also has a weight. The ii-th plate is the path from vertex aia_i to vertex bib_i (since it is a tree, the path from aia_i to bib_i is unique), with weight cic_i.

Next, qq fruits will fall one by one; each fruit is essentially also a path. The ii-th fruit is the path from vertex uiu_i to vertex viv_i.

Each time, Yuuka needs to choose one plate to catch the current fruit: a plate can catch a fruit if and only if the plate’s path is a subpath of the fruit’s path. By convention, the path from aa to bb is considered the same as the path from bb to aa.

To increase the difficulty, for the ii-th fruit, among all plates that can catch it, you must choose the one with the kik_i-th smallest weight. Each plate can be reused without limit: after catching one fruit, it can continue to catch other fruits as long as it is a subpath of the fruit’s path. Yuuka thinks this game is hard; can you solve it easily for her?

Input Format

  • The first line contains three integers nn, pp, and qq, denoting the size of the tree, the number of plates, and the number of fruits.
  • The next n1n-1 lines each contain two integers aa and bb, indicating that there is an edge between aa and bb in the tree. The vertices are labeled from 11 to nn.
  • The next pp lines each contain three integers aa, bb, and cc, describing a plate whose path is from aa to bb with weight cc, where aba \ne b.
  • The next qq lines each contain three integers uu, vv, and kk, describing a fruit whose path is from uu to vv, where uvu \ne v. You need to choose the kk-th smallest plate, and the kk-th smallest is guaranteed to exist.

Output Format

For each fruit, output one line containing the weight of the chosen plate.

10 10 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
3 2 217394434
10 7 13022269
6 7 283254485
6 8 333042360
4 6 442139372
8 3 225045590
10 4 922205209
10 8 808296330
9 2 486331361
4 9 551176338
1 8 5
3 8 3
3 8 4
1 8 3
4 8 1
2 3 1
2 3 1
2 3 1
2 4 1
1 4 1
442139372 
333042360 
442139372 
283254485 
283254485 
217394434 
217394434 
217394434 
217394434 
217394434

Hint

For 100%100\% of the testdata, 1n,p,q4×1041 \le n, p, q \le 4 \times 10^4, 0c1090 \le c \le 10^9.

Translated by ChatGPT 5