Wesley's Anger Contest 5 Problem 6 - Squirrel Cities 2

View as PDF

Submit solution


Points: 30 (partial)
Time limit: 2.0s
Memory limit: 512M

Author:
Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

After building an advanced highway system, the squirrel nation is now preparing to establish an efficient urban road system within the cities.

The highway system is already established, with N - 1 bidirectional highways of a constant length connecting the N cities, with the i^{th} of these roads connecting cities u_i and v_i. It is possible to travel between any pair of cities using a series of highways. There are M different new local road proposals that they are considering. The i^{th} local road proposal connects cities x_i and y_i bidirectionally with the same length of 1 kilometre as all of the highways. To prevent overcomplicating the squirrel nation's travel maps, the highways and local roads combined will always form a cactus graph, where every highway and road will only belong to at most one simple cycle. There will also be no road proposals connecting a city to itself, and all road proposal are distinct. To save resources, the nation will only accept K of the M road proposals.

To establish the most efficient road system, the squirrel nation has taken into consideration the daily travel plans of all Q squirrels in the nation. The i^{th} squirrel travels from a_i to b_i on a daily basis using the shortest distance. The efficiency of a road system is then measured using the sum of the lengths of all Q squirrels' travel plans after the K local roads have been built. The length of a travel plan is defined as the minimum number of highways or local roads that the squirrel needs to travel on to get from a_i to b_i.

As the Squirrel Nation's head director, can you help select these K roads? The National Squirrel Council only likes to see fancy numbers, so you'll only need to tell them the minimum sum of shortest distances in kilometres for the most efficient road system.

For this problem, Python users are recommended to use PYPY over CPython.

Constraints

For this problem, you will NOT be required to pass all the samples in order to receive points. In addition, all subtasks are disjoint, and you are NOT required to pass previous subtasks to earn points for a specific subtask.

For all subtasks:

1 \le N \le 200\,000
0 \le M \le \lfloor \frac{N - 1}{2} \rfloor
0 \le K \le M
1 \le Q \le 200\,000
1 \le u_i, v_i \le N for all 1 \le i \le N - 1
1 \le x_i, y_i \le N for all 1 \le i \le M
1 \le a_i, b_i \le N for all 1 \le i \le Q
Only input where it is possible to travel between any pair of cities using a series of highways is allowed.
Only input where the highways and local road proposals combined form a cactus graph is allowed. There will also be no road proposals connecting a city to itself, and all road proposal are distinct.

Subtask 1 [7%]

1 \le N \le 200
0 \le K \le \min(M, 1)
1 \le Q \le 200

Subtask 2 [5%]

1 \le N \le 200
1 \le Q \le 200

Subtask 3 [12%]

1 \le N \le 2\,000
0 \le K \le \min(M, 1)
1 \le Q \le 2\,000

Subtask 4 [5%]

1 \le N \le 2\,000
1 \le Q \le 2\,000

Subtask 5 [16%]

0 \le M \le \min(\lfloor \frac{N - 1}{2} \rfloor, 2\,500)
0 \le K \le \min(M, 1)
1 \le Q \le 5\,000

Subtask 6 [5%]

0 \le M \le \min(\lfloor \frac{N - 1}{2} \rfloor, 2\,500)
1 \le Q \le 5\,000

Subtask 7 [50%]

No additional constraints.

Input Specification

The first line of input contains 4 integers, N, M, K, and Q, representing the number of cities in the squrriel nation, the number of local road proposals, the number of proposal to accept, and the number of daily travel plans to take into consideration.

The next N - 1 lines describe the existing highway network. Each line contains 2 integers, u_i and v_i, indicating an existing highway between cities u_i and v_i.

The next M lines describe the local road proposals. Each line contains 2 integers, x_i and y_i, indicating a local road proposal between cities x_i and y_i.

The next Q lines describe the squirrels' travel plans. Each line contains 2 integers, a_i and b_i, indicating that the travel plan of a squirrel traveling from city a_i to b_i should be taken into consideration.

Output Specification

This problem is graded with an identical checker. This includes whitespace characters. Ensure that every line of output is terminated with a \n character and that there are no trailing spaces.

Output, on a single line, the minimum sum of shortest distances in kilometres for the most efficient road system after K local roads are built.

Sample Input 1

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

Sample Output 1

2

Sample Explanation 1

If a local road is built between cities 1 and 2, then the sum of shortest distances is 3 kilometres. A better choice is to build a local road between cities 3 and 7, where the sum of shortest distances is 2 kilometres.

Sample Input 2

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

Sample Output 2

7

Sample Explanation 2

The 3 proposals for local roads are 1-9, 5-6, and 8-4. Building the second and third proposals leads to the minimum sum of shortest distances, which is 1 + 1 + 1 + 1 + 3 = 7.


Comments

There are no comments at the moment.