아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Indispensable Overpass

면접 대비

메모리 제한1024 MB

요약
두 트리에 새로 잇는 지점마다 합쳐진 트리의 모든 정점 쌍 거리 평균을 구합니다.
난이도

보통10점 중 7점

유형
트리, 수학
정답자
아직 제출이 없습니다

문제

A modern railroad system built in Ekiya's town bumped into a major hurdle: the main freeway running north to south. W\mathbf{W} stations have already been built and connected on the western side of the freeway and E\mathbf{E} 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 ss and tt is a list of distinct stations that starts with ss, ends with tt, and such that any two consecutive stations on the list share a connection. The railroad system currently has W\mathbf{W} stations on the western side, connected through W−1\mathbf{W}-1 connections such that there is exactly one path between any two distinct western stations. Similarly, there are E\mathbf{E} eastern stations connected through E−1\mathbf{E}-1 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 W+E−1\mathbf{W}+\mathbf{E}-1 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 11).

As an example, the picture below illustrates a scenario with W=2\mathbf{W} = 2 stations on the west side and E=3\mathbf{E} = 3 stations on the east side. There are 22 possible overpasses shown.

This table shows the lengths of the paths between pairs of stations if each overpass were to be built.

1↔1\color{darkred}{\mathbf{1 \leftrightarrow 1}}2↔3\color{darkblue}{\mathbf{2 \leftrightarrow 3}}
West 11West 221111
West 11East 111133
West 11East 223333
West 11East 332222
West 22East 112222
West 22East 224422
West 22East 333311
East 11East 222222
East 11East 331111
East 22East 331111
Average:221.81.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, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case starts with a line with three integers W\mathbf{W}, E\mathbf{E}, and C\mathbf{C}, the number of western and eastern stations, and the number of options for the overpass connection, respectively. Western stations are numbered between 11 and W\mathbf{W} and eastern connections are numbered between 11 and E\mathbf{E}.

The second line of a test case contains W−1\mathbf{W}-1 integers X_1,X_2,…,X_W−1\mathbf{X\_1}, \mathbf{X\_2}, \dots, \mathbf{X\_{W-1}} representing that the ii-th existing connection among western stations connects western stations ii and X_i\mathbf{X\_i}.

The third line of a test case contains E−1\mathbf{E}-1 integers F_1,F_2,…,F_E−1\mathbf{F\_1}, \mathbf{F\_2}, \dots, \mathbf{F\_{E-1}} representing that the jj-th existing connection among eastern stations connects eastern stations jj and F_j\mathbf{F\_j}.

Finally, the last C\mathbf{C} lines of a test case describe the options for the overpass connection. The kk-th of these lines contains two integers A_k\mathbf{A\_k} and B_k\mathbf{B\_k} representing the western and eastern stations, respectively, that the kk-th option for an overpass connection would connect.

출력

For each test case, output one line containing Case #x: y1 y2 ... yC, where xx is the test case number (starting from 1) and y_ky\_k is the average distance of the map resulting in adding the kk-th option as an overpass connection to all existing connections.

y_1y\_1, y_2y\_2, …\dots and y_ky\_k will be considered correct if they are within an absolute or relative error of 10−610^{-6} of the correct answer.

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • 2≤W≤1052 \le \mathbf{W} \le 10^5.
  • 2≤E≤1052 \le \mathbf{E} \le 10^5.
  • i+1≤X_i≤Wi + 1 \le \mathbf{X\_i} \le \mathbf{W}, for all ii. (This implies that there is exactly one path between each pair of western stations.)
  • j+1≤F_j≤Ej + 1 \le \mathbf{F\_j} \le \mathbf{E}, for all jj. (This implies that there is exactly one path between each pair of eastern stations.)
  • 1≤A_k≤W1 \le \mathbf{A\_k} \le \mathbf{W}, for all kk.
  • 1≤B_k≤E1 \le \mathbf{B\_k} \le \mathbf{E}, for all kk.
  • (A_k,B_k)≠(A_ℓ,B_ℓ)(\mathbf{A\_k}, \mathbf{B\_k}) \neq (\mathbf{A\_\ell}, \mathbf{B\_\ell}), for all k≠ℓk \neq \ell. (Each listed overpass connection is different.)

힌트

Sample Case #1 is explained and illustrated in the problem statement. Sample Case #2 and Sample Case #3 are illustrated below.

예제1

  1. 예제 1

    입력
    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