#P15243. [NHSPC 2025] 融合圖的直徑

[NHSPC 2025] 融合圖的直徑

Problem Description

A graph structure G=(V,E)G = (V, E) consists of a finite set V(G)V(G) as the set of vertices, and a set E(G)E(G) 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 AA and BB, denoted in set theory as A×BA \times B, is the set of all possible ordered pairs, where the first element of the ordered pair is a member of AA and the second element is a member of BB. Professor RayRay, a graph theorist who has studied graph properties for many years, defined the Cartesian product graph of two graphs GG and HH as a new graph structure denoted by G×HG \times H, whose vertex set is V(G)×V(H)V(G) \times V(H). In this graph, two vertices (u,v)(u, v) and (u′,v′)(u^\prime, v^\prime) are adjacent if and only if:

  • u=u′u = u^\prime and {v,v′}∈E(H)\{v, v^\prime\} \in E(H), or
  • v=v′v = v^\prime and {u,u′}∈E(G)\{u, u^\prime\} \in E(G).

Figure 2 shows the Cartesian product of GG and HH in Figure 1.

:::align{center}

Figure 2 :::

To further understand the properties of the Cartesian product graph, Professor RayRay defined some measures: the distance between any two vertices xx and yy in a graph is the number of edges in the shortest path from xx to yy, i.e., the minimum number of edges among all paths from xx to yy. 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 GG and HH, please help Professor RayRay compute the diameter of the Cartesian product graph G×HG \times H. If the answer is greater than or equal to 109+710^9+7, output the remainder after dividing by 109+710^9+7. If there is no answer, meaning there exists a pair of vertices with no path between them, output −1-1.

:::align{center}

Figure 3: The diameter of this graph is 22 :::

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}$$
  • n1n_1 is the number of vertices in graph GG, i.e., ∣V(G)∣\left\vert V( G )\right\vert.
  • ei,je_{i,j} indicates whether ii and jj are adjacent in graph GG. ei,j=1e_{i,j}=1 means they are adjacent, and ei,j=0e_{i,j}=0 means they are not adjacent.
  • n2n_2 is the number of vertices in graph HH, i.e., ∣V(H)∣\left\vert V( H )\right\vert.
  • ei,j′e^\prime_{i,j} indicates whether ii and jj are adjacent in graph HH. ei,j′=1e^\prime_{i,j}=1 means they are adjacent, and ei,j′=0e^\prime_{i,j}=0 means they are not adjacent.

Output Format

DD
  • If the diameter exists, then DD is the remainder of the diameter of the Cartesian product graph G×HG \times H after dividing by 109+710^9+7.
  • If the diameter does not exist, then D=−1D = -1.
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

  • 1≤n1≤20001 \leq n_1 \leq 2000.
  • ei,j∈{0,1}e_{i,j} \in \lbrace 0, 1 \rbrace.
  • ∀1≤i<j≤n1,ei,j=ej,i\forall 1 \leq i < j \leq n_1, e_{i,j}=e_{j,i}.
  • It is guaranteed that graph GG has no self-loops, i.e., ∀1≤i≤n1,ei,i=0\forall 1\le i\le n_1, e_{i,i}=0.
  • 1≤n2≤20001 \leq n_2 \leq 2000.
  • ei,j′∈{0,1}e^\prime_{i,j} \in \lbrace 0, 1 \rbrace.
  • $\forall 1 \leq i < j \leq n_2, e^\prime_{i,j}=e^\prime_{j,i}$.
  • It is guaranteed that graph HH has no self-loops, i.e., ∀1≤i≤n2,ei,i′=0\forall 1\le i\le n_2, e^\prime_{i,i}=0.

Scoring

This problem has four subtasks, with the constraints shown below.
Let m1,m2m_1, m_2 be the number of edges in graphs GG and HH, 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 n1,m1≤400n_1, m_1 \leq 400, n2=1n_2=1 and m2=0m_2=0.
2 11 It is guaranteed that both GG and HH are connected acyclic graphs.
3 25 m1,m2≤4000m_1, m_2 \leq 4000.
4 46 No additional constraints.

Translated by ChatGPT 5