Educational Round #7 — Editorial
A directed graph with vertices and edges is given, with vertices numbered from to . Find the minimum number of edges that need to be reversed so that there exists at least one path from vertex to vertex .
Input. The first line contains two integers and m~(1 \le n, m \le 2 \cdot 10^6) — the number of vertices and edges in the graph. Each of the following lines contains two integers x_i and y_i~(1 \le x_i, y_i \le n), indicating that the -th directed edge goes from vertex x_i to vertex y_i.
Output. Print the minimum number of edges that need to be reversed. If it is not possible to obtain a path from vertex to vertex , print .

7 7
1 2
3 2
3 4
7 4
6 2
5 6
7 52The transportation system of the city of Baku consists of intersections and bidirectional roads connecting them. Each road connects exactly two intersections, and there can be at most one road between any pair of intersections. Moreover, it is possible to travel between any two intersections using the existing roads.
The distance between two intersections is defined as the minimum number of roads among all possible paths connecting them.

The city mayor has decided to improve the transportation system and has instructed the director of the transportation department to build a new road. However, the mayor recently bought a new car and enjoys driving from home to work and back every day. He does not want the distance between intersection , where his home is located, and intersection , where his workplace is located, to decrease after the new road is built.
Help the director of the transportation department determine how many pairs of unconnected intersections exist such that if a road is built between them, the distance between and will not decrease.
Input. The first line contains four integers:
n\ (1 \le n \le 10^3) — the number of intersections,
m\ (1 \le m \le 10^5) — the number of roads,
— the intersection where the mayor's home is located,
t\ (1 \le s, t \le n, s \ne t) — the intersection where the mayor's workplace is located.
The next lines each contain two integers u_i and v_i\ (1 \le u_i, v_i \le n, u_i \ne v_i), indicating that there is a bidirectional road between intersections u_i and v_i.
Output. Print the number of pairs of unconnected intersections such that adding a road between them will not decrease the distance between intersections and .

5 4 1 5
1 2
2 3
3 4
4 505 4 3 5
1 2
2 3
3 4
4 55Find the weight of the minimum spanning tree for an undirected weighted connected graph.
Input. The first line contains the number of vertices and the number of edges (1 \leq n \leq 100, 1 \leq m \leq 6000). Each of the following lines contains three integers , , , where and are the vertex numbers connected by an edge, and is the weight of the edge (a positive integer not exceeding ).
Output. Print the weight of the minimum spanning tree.

3 3
1 2 1
2 3 2
3 1 33You need to cut a wooden stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money based on the length of the stick being cut. The procedure requires making only one cut at a time.
It is easy to see that different choices of the cutting order lead to different costs. For example, consider a stick of length meters that needs to be cut at , , and meters from one end. Consider a few options:
You could cut first at meters, then at meters, and finally at meters. This would result in a cost of 10 + 8 + 6 = 24, because the first piece was meters, the second meters, and the third meters.
In another option, if you first cut at meters, then at meters, and finally at meters, the cost would be 10 + 4 + 6 = 20, which is the better price.
Your boss trusts your computing skills to determine the minimal cost of cutting the given stick.
Input. The input consists of several test cases. The first line of each test case contains the length of the stick (l < 1000) that needs to be cut. The next line contains the number of cuts (n < 50) to be made.
The following line contains positive integers c_i (0 < c_i < l), representing the positions for the cuts, given in strictly increasing order. A line with l = 0 denotes the end of the input data.
Output. Print the minimal cost of cutting the stick in the exact format given in the example.

100
3
25 50 75
10
4
4 5 7 8
0The minimum cutting is 200.
The minimum cutting is 22.Near Pemberley Villa, located in the southern district of Byteland, there is a large pasture. Mrs. Darcy is concerned about her delicate plants, which could be trampled by strangers. Therefore, she decided to enclose certain areas of the pasture with triangular fences.
In Mrs. Darcy's basement, there are several fences. She forms each triangular area using exactly three fences, so that each side of the triangle corresponds to one fence. The fences are sturdy and beautiful, so she will not combine multiple fences to form a single side, nor will she cut a fence into shorter pieces. Mrs. Darcy's goal is to enclose the largest possible total area of the pasture.
Input. Each line contains a separate test case. The first number in the line is the number of fences n~(n \le 16) that Mrs. Darcy has. The next integers, each between and , are the lengths of these fences.
Output. For each test case, print in a separate line the maximum area that can be enclosed using the available fences. The answer must be printed with decimal digits.
Examples. In the first test case, it is optimal to construct triangles with sides (4, 5, 6) and (7, 8, 9). The total enclosed area in this case is .
In the second test case, the answer is , since it is impossible to form any triangle.
In the third test case, a triangle with sides (4, 4, 4) should be built. Note that fence lengths can repeat.
7 3 4 5 6 7 8 9
4 1 2 4 8
4 7 4 4 436.7544
0.0000
6.9282Vasya received a rectangular matrix of size n \times m as a gift from his mother. Each cell of the matrix contains an integer. At first, Vasya enthusiastically played various mathematical games with the matrix: sometimes he would swiftly compute its determinant, and sometimes he would easily raise it to different powers.
However, he eventually got bored of such games and came up with a new pastime: Vasya chooses an integer and tries to find a submatrix of maximum area such that the sum of all its elements does not exceed . A submatrix is defined as a rectangular fragment of the original matrix.
Input. The first line contains three integers: , , and k~(1 \le n, m \le 300,\ 1 \le k \le 10^9).
The next lines each contain non-negative integers, each of which does not exceed .
Output. Print a single integer — the area of the largest submatrix whose sum of elements does not exceed .
1 3 4
8 6 413 3 12
7 5 7
8 4 8
4 3 23
Comments