#P4281. [AHOI2008] 紧急集合 / 聚会

    ID: 3215 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2008倍增各省省选安徽最近公共祖先,LCA树链剖分,树剖

[AHOI2008] 紧急集合 / 聚会

Description

On Happy Island there is a fun game called "Emergency Assembly." There are nn waiting points scattered around the island, connected by n1n-1 roads. Each road connects two waiting points, and by these roads one can reach all waiting points. Moving along a road from one point to another costs one game coin.

Players participate in teams of three. At the start, all players are arbitrarily located at various waiting points (each point may have multiple players waiting at the same time). Every player carries enough game coins (to pay the road costs), a map (showing the road connections between the waiting points), and a communicator (to contact their teammates). When the assembly signal sounds, teammates quickly contact each other, learn the waiting points of all members in their team, and then promptly choose one meeting point among the nn waiting points. All team members will gather at that meeting point. The team with the smallest total cost of gathering will be the winner.

Xiao Keke and friends invite you to join the game, and you are in charge of choosing the meeting point. Can you smartly complete this task and help Xiao Keke win?

Input Format

The first line contains two positive integers nn and mm, denoting the number of waiting points (waiting points are numbered from 11 to nn) and the number of gatherings that must be completed to win a prize.

Then follow n1n-1 lines. Each of these lines contains two positive integers a,ba, b, indicating there is a road between waiting point aa and waiting point bb.

Then follow mm lines. Each line contains three positive integers x,y,zx, y, z, indicating, before a particular gathering, the waiting points occupied by Xiao Keke, Xiao Keke’s friend, and you, respectively.

Output Format

Output mm lines. Each line should contain two integers p,cp, c separated by a space. The ii-th line indicates that for the ii-th gathering, the chosen meeting point is the waiting point numbered pp, and the total cost of gathering is cc game coins.

6 4  
1 2  
2 3  
2 4 
4 5
5 6
4 5 6
6 3 1
2 4 4 
6 6 6
5 2
2 5
4 1
6 0

Hint

  • For 40%40\% of the testdata, n2×103n \leq 2 \times 10^3, m2×103m \leq 2 \times 10^3.
  • For 100%100\% of the testdata, 1x,y,zn5×1051 \leq x, y, z \leq n \leq 5 \times 10^5, 1m5×1051 \leq m \leq 5 \times 10^5.

Translated by ChatGPT 5