# 洛谷 P2680 运输计划

## Description:

L 国有 $n$ 个星球，还有 $n-1$ 条双向航道，每条航道建立在两个星球之间，这 $n-1$ 条航道连通了 $L$ 国的所有星球。

# 洛谷 P3128 [USACO15DEC]最大流Max Flow

## Description:

Farmer John has installed a new system of $N-1$ pipes to transport milk between the $N$ stalls in his barn ($2 \leq N \leq 50,000$), conveniently numbered $1 \ldots N$. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between $K$ pairs of stalls ($1 \leq K \leq 100,000$). For the $i$th such pair, you are told two stalls $s_i$ and $t_i$, endpoints of a path along which milk is being pumped at a unit rate. FJ is concerned that some stalls might end up overwhelmed with all the milk being pumped through them, since a stall can serve as a waypoint along many of the $K$ paths along which milk is being pumped. Please help him determine the maximum amount of milk being pumped through any stall. If milk is being pumped along a path from $s_i$ to $t_i$, then it counts as being pumped through the endpoint stalls $s_i$ and $t_i$, as well as through every stall along the path between them.

FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道，隔间编号从1到N。所有隔间都被管道连通了。

FJ有K(1≤K≤100,000)条运输牛奶的路线，第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力，你需要计算压力最大的隔间的压力是多少。

# LibreOJ #2332. 「JOI 2017 Final」焚风现象

## Description:

IOI 王国永远刮着海风。风从地点 $0$ 依次吹到地点 $1$，地点 $2$ ……直到地点 $N$，共 $N+1$ 个地点。JOI 君住在地点 $N$。地点 $0$ 的海拔 $A_0=0$，地点 $i(1\leqslant i\leqslant N)$ 的海拔为 $A_i$。

• 如果 $A_i=A_{i+1}$，风的温度不变；
• 如果 $A_i<A_{i+1}$，风每爬升 $1$ 米，温度就会下降 $S$ 度；
• 如果 $A_i> A_{i+1}$，风每下沉 $1$ 米，温度就会升高 $T$ 度。

IOI 国的地壳运动很强烈。你得到了 $Q$ 天来地壳运动的数据。在第 $j$ 日 $(1\leqslant j\leqslant Q)$，地点 $L_j, L_j+1, \ldots, R_j (1\leqslant L_j\leqslant R_j\leqslant N)$ 的海拔升高了 $X_j$，注意 $X_j$ 可能是负数。

# 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.

# Aizu 1309 The Two Men of the Japanese Alps

## Description:

Two experienced climbers are planning a first-ever attempt: they start at two points of the equal altitudes on a mountain range, move back and forth on a single route keeping their altitudes equal, and finally meet with each other at a point on the route. A wise man told them that if a route has no point lower than the start points (of the equal altitudes) there is at least a way to achieve the attempt. This is the reason why the two climbers dare to start planning this fancy attempt.

The two climbers already obtained altimeters (devices that indicate altitude) and communication devices that are needed for keeping their altitudes equal. They also picked up a candidate route for the attempt: the route consists of consequent line segments without branches; the two starting points are at the two ends of the route; there is no point lower than the two starting points of the equal altitudes. An illustration of the route is given in Figure E.1 (this figure corresponds to the first dataset of the sample input).

Figure E.1: An illustration of a route

The attempt should be possible for the route as the wise man said. The two climbers, however, could not find a pair of move sequences to achieve the attempt, because they cannot keep their altitudes equal without a complex combination of both forward and backward moves. For example, for the route illustrated above: a climber starting at p1 (say A) moves to s, and the other climber (say B) moves from p6 to p5; then A moves back to t while B moves to p4; finally A arrives at p3 and at the same time B also arrives at p3. Things can be much more complicated and thus they asked you to write a program to find a pair of move sequences for them.

There may exist more than one possible pair of move sequences, and thus you are requested to find the pair of move sequences with the shortest length sum. Here, we measure the length along the route surface, i.e., an uphill path from (0, 0) to (3, 4) has the length of 5.