Under construction

Still being built. Things may change

Closing Time

Time limit1sMemory limit1024 MB

Summary
Given a weighted tree and cities X and Y, pick nonnegative closing times with sum at most K to maximize reachable cities from X plus from Y.
Level

Hard9 of 10

Topics
Tree, Binary search, Greedy
Solved
No attempts yet

Problem

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 p0,p1,…,plp_0 , p_1 , \dots , p_l such that:

  • p0=ap_0 = a,
  • pl=bp_l = b,
  • for each ii (0≤i<l0 ≤ i < l), there is a road connecting cities pip_i and pi+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 p0,p1,…,ptp_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 p0,…,ptp_0 , \dots , p_t between these two cities (so in particular p0=ap_0 = a and pt=bp_t = b) satisfies the following conditions:

  • the length of the path p0,p1p_0, p_1 is at most c\[p1]c\[p_1], and
  • the length of the path p0,p1,p2p_0, p_1, p_2 is at most c\[p2]c\[p_2], and
  • …\dots
  • the length of the path p0,p1,p2,…,ptp_0 , p_1 , p_2 ,\dots , p_t is at most c\[pt]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.

Constraints

  • 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.
  • SN≤200 000S_N ≤ 200\,000, where SNS_N is the sum of NN over all calls to max_score in each test case.

Examples1

  1. Example 1

    Input
    2 0 1 0
    0 1 5
    
    Expected output
    2