Closing Time

시간 제한1초메모리 제한1024 MB

요약
가중치가 있는 트리에서 닫는 시간의 합이 K 이하가 되도록 배정해, X와 Y에서 각각 도달 가능한 도시 수의 합을 최대로 만든다.
난이도

어려움10점 중 8점

유형
트리, DFS, 그리디, 정렬
정답자
아직 제출이 없습니다

문제

Hungary is a country with NN cities, numbered from 00 to N−1N - 1.

The cities are connected by N−1N - 1 bidirectional roads, numbered from 00 to N−2N - 2. Road jj (0≤j≤N−20 ≤ j ≤ N - 2) connects city U\[j]U\[j] and city V\[j]V\[j] and has length T\[j]T\[j], that is, it allows one to travel between the cities in T\[j]T\[j] units of time. Each road connects two different cities, and each pair of cities is connected by at most one road.

A path between two distinct cities aa and bb is a sequence of distinct cities p_0,p_1,…,p_lp\_0 , p\_1 , \dots , p\_l such that:

  • p_0=ap\_0 = a,
  • p_l=bp\_l = b,
  • for each ii (0≤i<l0 ≤ i < l), there is a road connecting cities p_ip\_i and p_i+1p\_{i+1}.

It is possible to travel from any city to any other city by using the roads, that is, there is a path between every two distinct cities. Note that the path connecting any pair of cities is unique.

The length of a path p_0,p_1,…,p_tp\_0 , p\_1 , \dots, p\_t is the sum of the lengths of the tt roads connecting consecutive cities along the path.

In Hungary, many people travel to attend the Foundation Day festivities in two major cities. Once the celebrations are over, they return to their homes. The government wants to prevent the crowd from disturbing the locals, so they plan to lock down all cities at certain times. Each city will be assigned a non-negative closing time by the government. The government has decided that the sum of all closing times must not be more than KK. More precisely, for every ii between 00 and N−1N - 1, inclusive, the closing time assigned to city ii is a nonnegative integer c\[i]c\[i]. The sum of all c\[i]c\[i] must not be greater than KK.

Consider a city aa and some assignment of closing times. We say that a city bb is reachable from city aa if and only if either b=ab = a, or the path p_0,…,p_tp\_0 , \dots , p\_t between these two cities (so in particular p_0=ap\_0 = a and p_t=bp\_t = b) satisfies the following conditions:

  • the length of the path p_0,p_1p\_0, p\_1 is at most c\[p_1]c\[p\_1], and
  • the length of the path p_0,p_1,p_2p\_0, p\_1, p\_2 is at most c\[p_2]c\[p\_2], and
  • …\dots
  • the length of the path p_0,p_1,p_2,…,p_tp\_0 , p\_1 , p\_2 ,\dots , p\_t is at most c\[p_t]c\[p\_t].

This year, the two main festival sites are located in city XX and city YY. For each assignment of closing times, the convenience score is defined as the sum of the following two numbers:

  • The number of cities reachable from city XX.
  • The number of cities reachable from city YY.

Note that if a city is reachable from city XX and reachable from city YY, it counts twice towards the convenience score.

Your task is to compute the maximum convenience score that can be achieved by some assignment of closing times.

제한

  • 2≤N≤200,0002 ≤ N ≤ 200\\,000
  • 0≤X<Y<N0 ≤ X < Y < N
  • 0≤K≤10180 ≤ K ≤ 10^{18}
  • 0≤U\[j]<V\[j]<N0 ≤ U\[j] < V\[j] < N (for each jj such that 0≤j≤N−20 ≤ j ≤ N - 2)
  • 1≤W\[j]≤1061 ≤ W\[j] ≤ 10^6 (for each jj such that 0≤j≤N−20 ≤ j ≤ N - 2)
  • It is possible to travel from any city to any other city by using the roads.
  • S_N≤200,000S\_N ≤ 200\\,000, where S_NS\_N is the sum of NN over all calls to max_score in each test case.

예제

이 문제는 공개된 예제가 없습니다.