#P17418. [ICPC 2018 Xuzhou R] Rikka with Minimum Spanning Trees

    ID: 19920 远端评测题 6000ms 512MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>2018并查集生成树概率论ICPC

[ICPC 2018 Xuzhou R] Rikka with Minimum Spanning Trees

Problem 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\textbf{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 k1k_1 and k2k_2. Supposing the number of vertices and edges in the graph are nn and mm respectively, the following code in C++ tells you how to generate the graph and store the ii-th edge between the vertex u[i]u[i] and v[i]v[i] with weight w[i]w[i] in corresponding arrays. You can use the code directly in your submissions.

unsigned long long k1, k2;

unsigned long long xorShift128Plus() {
    unsigned long long k3 = k1, k4 = k2;
    k1 = k4;
    k3 ^= k3 << 23;
    k2 = k3 ^ k4 ^ (k3 >> 17) ^ (k4 >> 26);
    return k2 + k4;
}

int n, m, u[100001], v[100001];
unsigned long long w[100001];

void gen() {
    scanf("%d%d%llu%llu", &n, &m, &k1, &k2);
    for(int i = 1; i <= m; ++i) {
        u[i] = xorShift128Plus() % n + 1;
        v[i] = xorShift128Plus() % n + 1;
        w[i] = xorShift128Plus();
    }
}

Also, to decrease the size of the output, your code should output the answer modulo (109+7)(10^9 + 7).

$\textbf{If you have already learned how to handle that,}$ $\textbf{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 GG with nn vertices excluding all loops, its Laplacian matrix Ln×nL_{n \times n} is defined as (D−A)(D - A), where DD is the degree matrix and AA is the adjacency matrix of the graph. More precisely, in the matrix LL the entry li,jl_{i, j} (i≠ji \ne j) equals to −m-m where mm is the number of edges between the ii-th vertex and the jj-th vertex, and Li,iL_{i, i} equals to the degree of the ii-th vertex. Next, construct a matrix L∗L^{\ast} by deleting any row and any column from LL, for example, deleting row 11 and column 11. The Kirchhoff's matrix tree theorem shows that the number of spanning trees is exactly the determinant of L∗L^{\ast}, 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 ii-th block of operations as GiG_i. Without loss of generality, let's consider the 00-th block which has no operation and let G0G_0 be an empty graph with nn isolated vertices. The ii-th block of operations squeezes vertices in Gi−1G_{i - 1} connected by edges in this block into a single vertex. The result is exactly the graph GiG_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 Format

The input contains several test cases, and the first line contains a single integer TT (1≤T≤1001 \le T \le 100), the number of test cases.

For each test case, the only line contains four integers nn (1≤n≤105)(1 \le n \le 10^5), mm (m=105m = 10^5), k1k_1 and k2k_2 (108≤k1,k2≤101210^8 \le k_1, k_2 \le 10^{12}), where k1k_1 and k2k_2 are chosen randomly except for the sample.

Output Format

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

1
2 100000 123456789 987654321
575673759

Hint

Since the generator code is only provided for C++, Rikka strongly suggests you all solve the problem using C or C++ instead of other programming languages.