#5122. Problem 1. Sum of Distances
Problem 1. Sum of Distances
Problem 1. Sum of Distances
USACO 2021 January Contest, Platinum
Bessie has a collection of connected, undirected graphs (). For each , has exactly () vertices labeled and () edges. Each may contain self-loops, but not multiple edges between the same pair of vertices.
Now Elsie creates a new undirected graph with vertices, each labeled by a -tuple where . In , two vertices and are connected by an edge if for all , and are connected by an edge in .
Define the
distance
between two vertices in 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 and every vertex in the same component as it in , modulo .
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains , the number of graphs.
Each graph description starts with and on a single line, followed by edges.
Consecutive graphs are separated by newlines for readability. It is guaranteed that and .
OUTPUT FORMAT (print output to the terminal / stdout):
The sum of the distances between vertex and every vertex that is reachable from it, modulo .
SAMPLE INPUT:
2 2 1 1 2 4 4 1 2 2 3 3 4 4 1
SAMPLE OUTPUT:
4
contains vertices, of which are not connected to vertex . There are vertices that are distance away from and that is distance away. So the answer is .
SAMPLE INPUT:
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
SAMPLE OUTPUT:
706
contains vertices, all of which are connected to vertex . The number of vertices that are distance away from for each is given by the -th element of the following array: .
SCORING: Test cases 3-4 satisfy .Test cases 5-10 satisfy .Test cases 11-20 satisfy no additional constraints.
Problem credits: Benjamin Qi