#P7293. [USACO21JAN] Sum of Distances P

    ID: 8193 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP线段树USACO2021O2优化组合数学容斥原理扫描线

[USACO21JAN] Sum of Distances P

Problem Description

Bessie has some undirected connected graphs G1,G2,,GKG_1,G_2,\dots,G_K (2K51042 \le K \le 5 \cdot 10^4). For each 1iK1 \le i \le K, GiG_i has NiN_i (Ni2N_i \ge 2) vertices numbered 1Ni1 \dots N_i and MiM_i (MiNi1M_i \ge N_i - 1) edges. GiG_i may contain self-loops, but there are no multiple edges between the same pair of vertices.

Now Elsie builds a new undirected graph GG with N1N2NKN_1 \cdot N_2 \cdots N_K vertices. Each vertex is labeled by a KK-tuple (j1,j2,,jK)(j_1,j_2,\dots,j_K), where 1jiNi1 \le j_i \le N_i. If for all 1iK1 \le i \le K, jij_i and kik_i are connected by an edge in GiG_i, then there is an edge in GG between vertices (j1,j2,,jK)(j_1,j_2,\dots,j_K) and (k1,k2,,kK)(k_1,k_2,\dots,k_K).

Define the distance between two vertices in the same connected component of GG as the minimum number of edges on a path from one to the other. Compute the sum of distances from vertex (1,1,,1)(1,1,\dots,1) to all vertices in the same connected component as it, modulo 109+710^9+7.

Input Format

The first line contains KK, the number of graphs.

The first line of each graph description contains NiN_i and MiM_i, followed by MiM_i edges.

For readability, there is a blank line between adjacent graphs. The input guarantees that Ni105∑N_i \le 10^5 and Mi2105∑M_i \le 2 \cdot 10^5.

Output Format

Output the sum of distances from vertex (1,1,,1)(1,1,\dots,1) to all vertices reachable from it, modulo 109+710^9+7.

2

2 1
1 2

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

4 4
1 2
2 3
3 1
3 4

6 5
1 2
2 3
3 4
4 5
5 6

7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
706

Hint

Sample 1 Explanation

GG contains 24=82 \cdot 4 = 8 vertices, and 44 of them are not connected to vertex (1,1)(1,1). There are 22 vertices at distance 11 from (1,1)(1,1), and 11 vertex at distance 22. So the answer is 21+12=42 \cdot 1 + 1 \cdot 2 = 4.

Sample 2 Explanation

GG contains 467=1684 \cdot 6 \cdot 7 = 168 vertices, all connected to vertex (1,1,1)(1,1,1). For each i[1,7]i \in [1,7], the number of vertices at distance ii from (1,1,1)(1,1,1) is the ii-th element of the following array: [4,23,28,36,40,24,12][4,23,28,36,40,24,12].

Testdata Properties

  • Testdata 343-4 satisfies Ni300∏N_i \le 300.
  • Testdata 5105-10 satisfies Ni300∑N_i \le 300.
  • Testdata 112011-20 has no additional restrictions.

Problem by: Benjamin Qi.

Translated by ChatGPT 5