#P7293. [USACO21JAN] Sum of Distances P
[USACO21JAN] Sum of Distances P
Problem Description
Bessie has some undirected connected graphs (). For each , has () vertices numbered and () edges. may contain self-loops, but there are no multiple edges between the same pair of vertices.
Now Elsie builds a new undirected graph with vertices. Each vertex is labeled by a -tuple , where . If for all , and are connected by an edge in , then there is an edge in between vertices and .
Define the distance between two vertices in the same connected component of as the minimum number of edges on a path from one to the other. Compute the sum of distances from vertex to all vertices in the same connected component as it, modulo .
Input Format
The first line contains , the number of graphs.
The first line of each graph description contains and , followed by edges.
For readability, there is a blank line between adjacent graphs. The input guarantees that and .
Output Format
Output the sum of distances from vertex to all vertices reachable from it, modulo .
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
contains vertices, and of them are not connected to vertex . There are vertices at distance from , and vertex at distance . So the answer is .
Sample 2 Explanation
contains vertices, all connected to vertex . For each , the number of vertices at distance from is the -th element of the following array: .
Testdata Properties
- Testdata satisfies .
- Testdata satisfies .
- Testdata has no additional restrictions.
Problem by: Benjamin Qi.
Translated by ChatGPT 5