#P15243. [NHSPC 2025] 融合圖的直徑
[NHSPC 2025] 融合圖的直徑
Problem Description
A graph structure consists of a finite set as the set of vertices, and a set of unordered pairs as the set of edges (as shown in Figure 1). Graph structures have very wide applications, such as transportation networks, protein structure analysis, project management evaluation, urban system structure analysis, and routing for component placement in semiconductor chip design. Therefore, graph structures have always been a useful tool for mathematicians and computer scientists to solve problems.
:::align{center}

Figure 1 :::
In mathematics, the Cartesian product of two sets and , denoted in set theory as , is the set of all possible ordered pairs, where the first element of the ordered pair is a member of and the second element is a member of . Professor , a graph theorist who has studied graph properties for many years, defined the Cartesian product graph of two graphs and as a new graph structure denoted by , whose vertex set is . In this graph, two vertices and are adjacent if and only if:
- and , or
- and .
Figure 2 shows the Cartesian product of and in Figure 1.
:::align{center}

Figure 2 :::
To further understand the properties of the Cartesian product graph, Professor defined some measures: the distance between any two vertices and in a graph is the number of edges in the shortest path from to , i.e., the minimum number of edges among all paths from to . To compute the diameter of a graph, one must first find the shortest paths between every pair of vertices. Among all these shortest paths, the one with the greatest length is the diameter of the graph (as shown in Figure 3). Given two graphs and , please help Professor compute the diameter of the Cartesian product graph . If the answer is greater than or equal to , output the remainder after dividing by . If there is no answer, meaning there exists a pair of vertices with no path between them, output .
:::align{center}

Figure 3: The diameter of this graph is :::
Input Format
$$\begin{aligned} &n_1 \\ &e_{1,1} e_{1,2} \dots e_{1,n_1} \\ &e_{2,1} e_{2,2} \dots e_{2,n_1} \\ &\vdots \\ &e_{n_1,1} e_{n_1,2} \dots e_{n_1,n_1} \\ &n_2 \\ &e^\prime_{1,1} e^\prime_{1,2} \dots e^\prime_{1,n_2} \\ &e^\prime_{2,1} e^\prime_{2,2} \dots e^\prime_{2,n_2} \\ &\vdots \\ &e^\prime_{n_2,1} e^\prime_{n_2,2} \dots e^\prime_{n_2,n_2} \end{aligned}$$- is the number of vertices in graph , i.e., .
- indicates whether and are adjacent in graph . means they are adjacent, and means they are not adjacent.
- is the number of vertices in graph , i.e., .
- indicates whether and are adjacent in graph . means they are adjacent, and means they are not adjacent.
Output Format
- If the diameter exists, then is the remainder of the diameter of the Cartesian product graph after dividing by .
- If the diameter does not exist, then .
2
01
10
2
01
10
2
4
0101
1010
0101
1010
4
0100
1011
0100
0100
4
5
01000
10101
01010
00101
01010
1
0
3
Hint
Constraints
- .
- .
- .
- It is guaranteed that graph has no self-loops, i.e., .
- .
- .
- $\forall 1 \leq i < j \leq n_2, e^\prime_{i,j}=e^\prime_{j,i}$.
- It is guaranteed that graph has no self-loops, i.e., .
Scoring
This problem has four subtasks, with the constraints shown below.
Let be the number of edges in graphs and , respectively, i.e., $m_1=\left\vert E(G)\right\vert, m_2=\left\vert E(H)\right\vert$.
Each subtask may contain one or more testdata files. You will get the score for a subtask only if you solve all testdata in that subtask.
| Subtask | Score | Additional Input Constraints |
|---|---|---|
| 1 | 18 | , and . |
| 2 | 11 | It is guaranteed that both and are connected acyclic graphs. |
| 3 | 25 | . |
| 4 | 46 | No additional constraints. |
Translated by ChatGPT 5