# A. Rikka with Minimum Spanning Trees

## Description:

Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a problem about the minimum spanning tree (MST). I promise you all that this should be the easiest problem for most people.
A minimum spanning tree, or minimum weight spanning tree, is a subset of edges from an edge-weighted undirected graph, which forms a tree with the minimum possible total edge weight that connects all the vertices together without any cycles.
In this problem, Rikka wants you to calculate the summation of total edge weights through all MSTs for a given graph, which obviously equals to the product of the total edge weight in an MST and the total number of different MSTs. Note that two spanning trees are different if the sets of their edges are different. In addition, a disconnected graph could have no MSTs, the number of whose different MSTs is zero.
To decrease the size of the input, Rikka provides an edge-weighted undirected graph via a random number generator with given random seeds, denoted by two integers $k_1$ and $k_2$. Supposing the number of vertices and edges in the graph are $n$ and $m$ respectively, the following code in C++ tells you how to generate the graph and store the $i$-th edge between the vertex $u[i]$ and $v[i]$ with weight $w[i]$ in corresponding arrays. You can use the code directly in your submissions.

Also, to decrease the size of the output, your code should output the answer modulo $(10^9 + 7)$.
If you have already learned how to handle that, start your show and omit all the rest of the statement.
To make sure everyone knows how to solve this problem, here Rikka would like to provide for you all an effective practice which can solve the problem and help you all get Accepted!
The first one you need to know is the Kirchhoff’s matrix tree theorem. Given an undirected graph $G$ with $n$ vertices excluding all loops, its Laplacian matrix $L_{n \times n}$ is defined as $(D - A)$, where $D$ is the degree matrix and $A$ is the adjacency matrix of the graph. More precisely, in the matrix $L$ the entry $l_{i, j}$ ($i \ne j$) equals to $-m$ where $m$ is the number of edges between the $i$-th vertex and the $j$-th vertex, and $L_{i, i}$ equals to the degree of the $i$-th vertex. Next, construct a matrix $L^{*}$ by deleting any row and any column from $L$, for example, deleting row $1$ and column $1$. The Kirchhoff’s matrix tree theorem shows that the number of spanning trees is exactly the determinant of $L^{*}$, which can be computed in polynomial time.
Now let me explain an algorithm that counts the number of MSTs. The algorithm breaks up the Kruskal’s algorithm for MST into a series of blocks, each of which consists of a sequence of operations about adding edges in a same weight into a multigraph (where a multigraph is a graph, two vertices of which may be connected by more than one edge) whose vertices are components that have been built through the previous block of operations.
Precisely speaking, let’s label the multigraph that has been built after the $i$-th block of operations as $G_i$. Without loss of generality, let’s consider the $0$-th block which has no operation and let $G_0$ be an empty graph with $n$ isolated vertices. The $i$-th block of operations squeezes vertices in $G_{i - 1}$ connected by edges in this block into a single vertex. The result is exactly the graph $G_i​$.
If you know the cardinal principle of Kruskal’s algorithm pretty well, you may find that the number of MSTs is the product of the numbers of spanning trees in every component of the graph for each block-defining weight. Actually, the number of edges for a certain weight is fixed in all MSTs, based on the greedy-choice strategy in Kruskal’s algorithm. Finally, the Kirchhoff’s matrix tree theorem helps you compute the numbers of spanning trees for graphs.

## Input:

The input contains several test cases, and the first line contains a single integer $T$ ($1 \le T \le 100$), the number of test cases.
For each test case, the only line contains four integers $n$ $(1 \le n \le 10^5)$, $m$ ($m = 10^5$), $k_1$ and $k_2$ ($10^8 \le k_1, k_2 \le 10^{12}$), where $k_1$ and $k_2$ are chosen randomly except for the sample.

## Output:

For each test case, output a single line with a single number, the answer modulo $(10^9 + 7)$.

## Sample Input:

1
2 100000 123456789 987654321

575673759

# C. Rikka with Consistency

## Description:

On the way to the Moscow, Rikka knows someone will replace her. Who is the guy? A devil to get in touch with her dark side, or an angel to rinse the shadow off her mind? However, Rikka knows that her successor has a special name, whose meaning in Chinese is Consistency. The process of replacement is so wonderful and sentimental, which is what you all must know.
Now, the only road from Beijing to Moscow is described as a broken line with $n$ segments in the $X$-$H$ plane. The $i$-th segment connects the points $(i - 1, h_{i - 1})$ and $(i, h_i)$, and $h_0 = h_n = 0$ are known. This figure is a topographic map showing the whole trip from Beijing to Moscow and its $H$ axis indicates the altitude. The distance of a path between two points is the length of the broken line between their corresponding points in the map.
At the outset of the trip, Rikka is in Beijing whose location in the $X$-$H$ plane is $(0, 0)$; Consistency, the guy who will replace Rikka, is in Moscow which is located at $(n, 0)$. Consistency always maintains consistent academic standards, a consistent life level, a consistent height of perspective and the altitude as what Rikka owns. This is why their heights are the same yesterday, today and forever.
Now Rikka wants you to calculate the minimum total distance they need (which is the total length of paths that Rikka and Consistency travel along). By the time that Rikka arrives in Moscow and Consistency arrives in Beijing as well, their replacement will be finished (and this is the ending which is also a new beginning).

## Input:

The input contains several test cases, and the first line contains a single integer $T$ ($1 \le T \le 500$), the number of test cases.
For each test case, the first line contains a single integer $n$ ($1 \le n \le 50$), the number of segments.
The second line contains $(n + 1)$ integers $h_0, h_1, \cdots, h_n$ ($0 \le h_i \le 50$), satisfying $h_0 = h_n = 0$.
The input guarantees that the paths for each test case always exist.

## Output:

For each test case, output a single line with a single number, the minimum total distance they need. Your answer is considered correct if its absolute or relative error does not exceed $10^{-9}$. Formally, let your answer be $a$, and Rikka’s answer be $b$. Your answer is considered correct if $\frac{|a - b|}{\max(1, |b|)} \le 10^{-9}$.

2
4
0 1 1 2 0
4
0 2 1 3 0

## Sample Output:

12.128990204491960
22.313624568639947

# G. Rikka with Intersections of Paths

## Description:

Rikka has a tree $T$ with $n$ vertices numbered from $1$ to $n$.
Meanwhile, Rikka has marked $m$ simple paths in $T$, the $i$-th of which is between the vertices $x_i$ and $y_i$, where some of them could be the same path.
Now, Rikka wants to know in how many different strategies she can select $k$ paths from the marked paths such that those selected paths share at least one common vertex.

## Input:

The input contains several test cases, and the first line contains a single integer $T$ ($1 \le T \le 200$), the number of test cases.
For each test case, the first line contains three integers $n$ ($1 \le n \le 3 \times 10^5$), the size of the tree $T$, $m$ ($2 \le m \le 3 \times 10^5$), the number of marked paths, and $k$ ($2 \le k \le m$).
The following $(n - 1)$ lines describe the tree $T$. Each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \ne v$), representing an edge between the vertices $u$ and $v$.
The following $m$ lines describe all marked simple paths in the tree. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$).
The input guarantees that the sum of $n$ and the sum of $m$ in all test cases are at most $2 \times 10^6$ respectively.

## Output:

For each test case, output a single line with a single integer, the number of different strategies meeting the requirement modulo $(10^9 + 7)$.

1
3 6 2
1 2
1 3
1 1
2 2
3 3
1 2
1 3
2 3

## Sample Output:

10

### 题目链接

$ans += C_{cnt[i]}^{k} - C_{cnt[i] - LCAcnt[i]}^{k}​$，其中$cnt[i]​$为经过顶点$i​$的路径数量（通过树上差分计算），$LCAcnt[i]​$为$LCA​$是$i​$顶点的路径数量。