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$.
($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$.
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$.
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
$G$ contains $2\cdot 4=8$ vertices, $4$ of which are not connected to vertex
$(1,1)$. There are $2$ vertices that are distance $1$ away from $(1,1)$ and
$1$ that is distance $2$ away. So the answer is $2\cdot 1+1\cdot 2=4$.
$(1,1)$. There are $2$ vertices that are distance $1$ away from $(1,1)$ and
$1$ that is distance $2$ away. So the answer is $2\cdot 1+1\cdot 2=4$.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted