题库练习 Sum of Distances--Platinum
← 上一题 下一题 →

A1012 | Sum of Distances--Platinum

来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0

题目描述

Bessie has a collection of connected, undirected graphs $G_1,G_2,\ldots,G_K$
($2\le K\le 5\cdot 10^4$). For each $1\le i\le K$, $G_i$ has exactly $N_i$
($N_i\ge 2$) vertices labeled $1\ldots N_i$ and $M_i$ ($M_i\ge N_i-1$) edges.
Each $G_i$ may contain self-loops, but not multiple edges between the same
pair of vertices.
Now Elsie creates a new undirected graph $G$ with $N_1\cdot N_2\cdots N_K$
vertices, each labeled by a $K$-tuple $(j_1,j_2,\ldots,j_K)$ where $1\le
j_i\le N_i$. In $G$, two vertices $(j_1,j_2,\ldots,j_K)$ and
$(k_1,k_2,\ldots,k_K)$ are connected by an edge if for all $1\le i\le K$,
$j_i$ and $k_i$ are connected by an edge in $G_i$.
Define the _distance_ between two vertices in $G$ that lie in the same
connected component to be the minimum number of edges along a path from one
vertex to the other. Compute the sum of the distances between vertex
$(1,1,\ldots,1)$ and every vertex in the same component as it in $G$, modulo
$10^9+7$.

输入格式

The first line contains $K$, the number of graphs.
Each graph description starts with $N_i$ and $M_i$ on a single line, followed
by $M_i$ edges.
Consecutive graphs are separated by newlines for readability. It is guaranteed
that $\sum N_i\le 10^5$ and $\sum M_i\le 2\cdot 10^5$.

输出格式

The sum of the distances between vertex $(1,1,\ldots,1)$ and every vertex that
is reachable from it, modulo $10^9+7$.

输入输出样例

输入 #1
2
2 1
1 2
4 4
1 2
2 3
3 4
4 1
输出 #1
4
C++ 编辑器
输入
输出