CF1583H.Omkar and Tours

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Omkar is hosting tours of his country, Omkarland! There are nn cities in Omkarland, and, rather curiously, there are exactly n1n-1 bidirectional roads connecting the cities to each other. It is guaranteed that you can reach any city from any other city through the road network.

Every city has an enjoyment value ee . Each road has a capacity cc , denoting the maximum number of vehicles that can be on it, and an associated toll tt . However, the toll system in Omkarland has an interesting quirk: if a vehicle travels on multiple roads on a single journey, they pay only the highest toll of any single road on which they traveled. (In other words, they pay maxt\max t over all the roads on which they traveled.) If a vehicle traverses no roads, they pay 00 toll.

Omkar has decided to host qq tour groups. Each tour group consists of vv vehicles starting at city xx . (Keep in mind that a tour group with vv vehicles can travel only on roads with capacity v\geq v .) Being the tour organizer, Omkar wants his groups to have as much fun as they possibly can, but also must reimburse his groups for the tolls that they have to pay. Thus, for each tour group, Omkar wants to know two things: first, what is the enjoyment value of the city yy with maximum enjoyment value that the tour group can reach from their starting city, and second, how much per vehicle will Omkar have to pay to reimburse the entire group for their trip from xx to yy ? (This trip from xx to yy will always be on the shortest path from xx to yy .)

In the case that there are multiple reachable cities with the maximum enjoyment value, Omkar will let his tour group choose which one they want to go to. Therefore, to prepare for all possible scenarios, he wants to know the amount of money per vehicle that he needs to guarantee that he can reimburse the group regardless of which city they choose.

输入格式

The first line contains two integers nn and qq ( 2n21052 \leq n \leq 2 \cdot 10^5 , 1q21051 \leq q \leq 2 \cdot 10^5 ), representing the number of cities and the number of groups, respectively.

The next line contains nn integers e1,e2,,ene_1, e_2, \ldots, e_n ( 1ei1091 \leq e_i \leq 10^9 ), where eie_i represents the enjoyment value for city ii .

The next n1n-1 lines each contain four integers aa , bb , cc , and tt ( 1a,bn1 \leq a,b \leq n , 1c1091 \leq c \leq 10^9 , 1t1091 \leq t \leq 10^9 ), representing an road between city aa and city bb with capacity cc and toll tt .

The next qq lines each contain two integers vv and xx ( 1v1091 \leq v \leq 10^9 , 1xn1 \leq x \leq n ), representing the number of vehicles in the tour group and the starting city, respectively.

输出格式

Output qq lines. The ii -th line should contain two integers: the highest possible enjoyment value of a city reachable by the ii -th tour group, and the amount of money per vehicle Omkar needs to guarantee that he can reimburse the ii -th tour group.

输入输出样例

  • 输入#1

    5 3
    2 2 3 3 3
    1 2 4 7
    1 3 2 8
    2 4 8 2
    2 5 1 1
    1 3
    9 5
    6 2

    输出#1

    3 8
    3 0
    3 2
  • 输入#2

    5 5
    1 2 3 4 5
    1 2 4 1
    1 3 3 1
    1 4 2 1
    2 5 1 1
    5 1
    4 1
    3 1
    2 1
    1 1

    输出#2

    1 0
    2 1
    3 1
    4 1
    5 1
  • 输入#3

    5 5
    1 2 2 2 2
    1 2 5 8
    1 3 6 3
    1 4 4 5
    1 5 7 1
    4 1
    5 1
    6 1
    7 1
    8 1

    输出#3

    2 8
    2 8
    2 3
    2 1
    1 0

说明/提示

A map of the first sample is shown below. For the nodes, unbolded numbers represent indices and bolded numbers represent enjoyment values. For the edges, unbolded numbers represent capacities and bolded numbers represent tolls.

For the first query, a tour group of size 11 starting at city 33 can reach cities 11 , 22 , 33 , 44 , and 55 . Thus, the largest enjoyment value that they can reach is 33 . If the tour group chooses to go to city 44 , Omkar will have to pay 88 per vehicle, which is the maximum.

For the second query, a tour group of size 99 starting at city 55 can reach only city 55 . Thus, the largest reachable enjoyment value is still 33 , and Omkar will pay 00 per vehicle.

For the third query, a tour group of size 66 starting at city 22 can reach cities 22 and 44 . The largest reachable enjoyment value is again 33 . If the tour group chooses to go to city 44 , Omkar will have to pay 22 per vehicle, which is the maximum.

A map of the second sample is shown below:

For the first query, a tour group of size 55 starting at city 11 can only reach city 11 . Thus, their maximum enjoyment value is 11 and the cost Omkar will have to pay is 00 per vehicle.

For the second query, a tour group of size 44 starting at city 11 can reach cities 11 and 22 . Thus, their maximum enjoyment value is 22 and Omkar will pay 11 per vehicle.

For the third query, a tour group of size 33 starting at city 11 can reach cities 11 , 22 , and 33 . Thus, their maximum enjoyment value is 33 and Omkar will pay 11 per vehicle.

For the fourth query, a tour group of size 22 starting at city 11 can reach cities 11 , 22 , 33 and 44 . Thus, their maximum enjoyment value is 44 and Omkar will pay 11 per vehicle.

For the fifth query, a tour group of size 11 starting at city 11 can reach cities 11 , 22 , 33 , 44 , and 55 . Thus, their maximum enjoyment value is 55 and Omkar will pay 11 per vehicle.

首页