#P12961. [GCJ Farewell Round #4] Indispensable Overpass
[GCJ Farewell Round #4] Indispensable Overpass
题目描述
A modern railroad system built in Ekiya's town bumped into a major hurdle: the main freeway running north to south. stations have already been built and connected on the western side of the freeway and on the eastern side. One more connection is needed between a western and an eastern station, but because the freeway is in the way, that connection needs to be built using an overpass.
Ekiya is assessing which stations would be most convenient to connect with the overpass. As part of that assessment, she wants to know how the average length (in number of stations) of a path within the system might change with each possible option.
A path between stations and is a list of distinct stations that starts with , ends with , and such that any two consecutive stations on the list share a connection. The railroad system currently has stations on the western side, connected through connections such that there is exactly one path between any two distinct western stations. Similarly, there are eastern stations connected through connections such that there is exactly one path between any two distinct eastern stations. After the overpass connection is built connecting one western and one eastern station, there will be exactly one path between any two distinct stations.
A complete map is a map that has total connections and exactly one path between any pair of stations. The average distance of a complete map is the average of the length of paths between all pairs of different stations. The length of a path is one less than the length of the list of stations that defines it (e.g., the path between directly connected stations has a length of 1).
As an example, the picture below illustrates a scenario with stations on the west side and stations on the east side. There are 2 possible overpasses shown.
This table shows the lengths of the paths between pairs of stations if each overpass were to be built.
West 1 | West 2 | 1 ↔ 1 | 2 ↔ 3 |
---|---|---|---|
West 1 | East 1 | 1 | 3 |
East 2 | 3 | ||
East 3 | 2 | 2 | |
West 2 | East 1 | ||
East 2 | 4 | ||
East 3 | 3 | 1 | |
East 1 | East 2 | 2 | |
East 3 | 1 | ||
East 2 | |||
Average: | 2 | 1.8 |
Given the current stations and connections, and a list of options for the overpass connection, help Ekiya by calculating the average distance of the map that would result if that option was the only overpass connection built.
输入格式
The first line of the input gives the number of test cases, . test cases follow. Each test case starts with a line with three integers , , and , the number of western and eastern stations, and the number of options for the overpass connection, respectively. Western stations are numbered between and and eastern connections are numbered between and .
The second line of a test case contains integers , , , representing that the -th existing connection among western stations connects western stations and .
The third line of a test case contains integers , , , representing that the -th existing connection among eastern stations connects eastern stations and .
Finally, the last lines of a test case describe the options for the overpass connection. The -th of these lines contains two integers and representing the western and eastern stations, respectively, that the -th option for an overpass connection would connect.
输出格式
For each test case, output one line containing Case #x:
, where is the test case number (starting from ) and is the average distance of the map resulting in adding the -th option as an overpass connection to all existing connections.
, , and will be considered correct if they are within an absolute or relative error of of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.
3
2 3 2
2
3 3
1 1
2 3
3 4 2
2 3
3 3 4
1 3
1 2
3 4 1
2 3
3 3 4
2 2
Case #1: 2.0 1.8
Case #2: 2.19047619 2.47619048
Case #3: 2.2857142857
提示
Sample Explanation
Sample Case #1 is explained and illustrated in the problem statement. Sample Case #2 and Sample Case #3 are illustrated below.
Limits
- .
- .
- .
- , for all . (This implies that there is exactly one path between each pair of western stations.)
- , for all . (This implies that there is exactly one path between each pair of eastern stations.)
- , for all .
- , for all .
- $(\mathbf{A}_{k}, \mathbf{B}_{k}) \neq (\mathbf{A}_{\ell}, \mathbf{B}_{\ell})$, for all . (Each listed overpass connection is different.)
Test Set 1 (5 Pts, Visible Verdict)
- Time limit: 20 seconds.
- .
Test Set 2 (7 Pts, Hidden Verdict)
- Time limit: 40 seconds.
- .