#abc309d. D - Add One Edge

D - Add One Edge

Score : 400400 points

问题描述

我们有一个无向图,包含 (N1+N2)(N_1+N_2) 个顶点和 MM 条边。对于 i=1,2,,Mi=1,2,\ldots,M,第 ii 条边连接顶点 aia_i 和顶点 bib_i

以下性质得到保证:

  • 对于所有整数 uuvv 满足 1u,vN11 \leq u,v \leq N_1,顶点 uu 和顶点 vv 相连。
  • 对于所有整数 uuvv 满足 N1+1u,vN1+N2N_1+1 \leq u,v \leq N_1+N_2,顶点 uu 和顶点 vv 相连。
  • 顶点 11 和顶点 (N1+N2)(N_1+N_2) 不相连。

考虑执行以下操作恰好一次:

  • 选择一个整数 uu,满足 1uN11 \leq u \leq N_1,以及一个整数 vv,满足 N1+1vN1+N2N_1+1 \leq v \leq N_1+N_2,并添加一条连接顶点 uu 和顶点 vv 的边。

我们可以证明,在结果图中顶点 11 和顶点 (N1+N2)(N_1+N_2) 总是相连的;设 dd 为从顶点 11 到顶点 (N1+N2)(N_1+N_2) 的最短路径(边的数量)长度。

找出通过添加适当边所能得到的最大可能的 dd 值。

“相连”的定义:在一个无向图中,若两个顶点 uuvv 存在一条从顶点 uu 到顶点 vv 的路径,则称这两个顶点是相连的。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have an undirected graph with (N1+N2)(N_1+N_2) vertices and MM edges. For i=1,2,,Mi=1,2,\ldots,M, the ii-th edge connects vertex aia_i and vertex bib_i.
The following properties are guaranteed:

  • Vertex uu and vertex vv are connected, for all integers uu and vv with 1u,vN11 \leq u,v \leq N_1.
  • Vertex uu and vertex vv are connected, for all integers uu and vv with N1+1u,vN1+N2N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 11 and vertex (N1+N2)(N_1+N_2) are disconnected.

Consider performing the following operation exactly once:

  • choose an integer uu with 1uN11 \leq u \leq N_1 and an integer vv with N1+1vN1+N2N_1+1 \leq v \leq N_1+N_2, and add an edge connecting vertex uu and vertex vv.

We can show that vertex 11 and vertex (N1+N2)(N_1+N_2) are always connected in the resulting graph; so let dd be the minimum length (number of edges) of a path between vertex 11 and vertex (N1+N2)(N_1+N_2).

Find the maximum possible dd resulting from adding an appropriate edge to add.

Definition of "connected" Two vertices uu and vv of an undirected graph are said to be connected if and only if there is a path between vertex uu and vertex vv.

Constraints

  • 1N1,N21.5×1051 \leq N_1,N_2 \leq 1.5 \times 10^5
  • 0M3×1050 \leq M \leq 3 \times 10^5
  • 1aibiN1+N21 \leq a_i \leq b_i \leq N_1+N_2
  • (ai,bi)(aj,bj)(a_i,b_i) \neq (a_j,b_j) if iji \neq j.
  • Vertex uu and vertex vv are connected for all integers uu and vv such that 1u,vN11 \leq u,v \leq N_1.
  • Vertex uu and vertex vv are connected for all integers uu and vv such that N1+1u,vN1+N2N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 11 and vertex (N1+N2)(N_1+N_2) are disconnected.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N1N_1 N2N_2 MM

a1a_1 b1b_1

\vdots

aMa_M bMb_M

Output

Print the answer.

Sample Input 1

3 4 6
1 2
2 3
4 5
4 6
1 3
6 7

Sample Output 1

5

If we set u=2u=2 and v=5v=5, the operation yields d=5d=5, which is the maximum possible.

Sample Input 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

Sample Output 2

4

update @ 2024/3/10 08:46:26