#P16708. [SEATST 2026] 麻烦的旅程 / Troublesome Trip

[SEATST 2026] 麻烦的旅程 / Troublesome Trip

Problem Description

A unique and mysterious species called Nuko lives on an archipelago in a remote corner of the world. The archipelago can be modeled as NN islands, numbered from 00 to N−1N - 1, connected by MM bridges. For all 0≤i≤M−10 \le i \le M - 1, bridge ii connects islands U[i]U[i] and V[i]V[i] in both directions. It is guaranteed that you can travel from any island to any other island. Each bridge connects two different islands, and no two bridges connect the same pair of islands.

In ancient times, Nukos lived only on island 00. However, as time passed, Nukos spread to all islands. Whenever a group of Nukos crosses a bridge and reaches a new island, they evolve and form a subspecies different from the one on the previous island. More precisely, for all 0≤j≤N−10 \le j \le N - 1, the Nukos on island jj belong to subspecies sjs_j, where sjs_j is the minimum number of bridges that must be crossed to reach island jj from island 00. For example, the Nukos on island 00 belong to subspecies 00.

You are a traveler and plan to use these bridges to travel from island AA to island BB. It is guaranteed that A≠BA \ne B. When you are on an island, you will inevitably encounter the Nuko subspecies living there. Since each subspecies has its own customs that you need to adapt to, and adapting to different customs can be troublesome, your goal is to choose a path that minimizes the number of distinct Nuko subspecies you encounter.

Can you compute the minimum possible number of distinct Nuko subspecies you must encounter when traveling from island AA to island BB?

Implementation Details

You need to implement the following function.

int min_distinct(int N, int M, int A, int B, std::vector<int> U, std::vector<int> V)
  • NN: the number of islands.
  • MM: the number of bridges.
  • AA: the starting island of your trip.
  • BB: the destination island of your trip.
  • UU, VV: arrays of length MM describing the bridges.
  • This function should return the minimum number of distinct Nuko subspecies you must encounter.

Input Format

N M A B
U[0] V[0]
U[1] V[1]
...
U[M - 1] V[M - 1]

Output Format

An integer, representing the return value of the min_distinct function.

Hint

Samples

Consider the following function call.

min_distinct(5, 5, 2, 4, [0, 1, 2, 3, 4], [1, 2, 3, 4, 0])

The diagram of the islands is as follows, where different shadings represent different Nuko subspecies.

:::align{center} :::

For sample 11, the optimal path is 2−3−42 - 3 - 4. The Nuko subspecies encountered are 11 and 22. Therefore, this call should return 22.

min_distinct(8, 9, 4, 7, [0, 0, 0, 1, 1, 2, 2, 6, 7], [1, 2, 3, 4, 5, 5, 6, 3, 3])

The diagram of the islands is as follows, where different shadings represent different Nuko subspecies.

:::align{center} :::

For sample 22, the optimal path is 4−1−5−2−6−3−74 - 1 - 5 - 2 - 6 - 3 - 7. The Nuko subspecies encountered are 11 and 22. Therefore, this call should return 22.

min_distinct(15, 17, 3, 7,
              [0, 1, 2, 3, 4, 13, 12, 12, 11, 10, 10, 9, 8, 7, 6, 8, 0],
              [1, 2, 3, 4, 13, 12, 1, 11, 10, 9, 5, 8, 7, 6, 5, 14, 14])

For sample 33, when traveling from island 33 to island 77, the minimum number of distinct Nuko subspecies you must encounter is 33. Therefore, this call should return 33.

Constraints

  • 2≤N≤5 000 0002 \le N \le 5\ 000\ 000.
  • 1≤M≤5 000 0001 \le M \le 5\ 000\ 000.
  • 0≤A,B≤N−10 \le A, B \le N - 1.
  • A≠BA \ne B.
  • For all 0≤i≤M−10 \le i \le M - 1, 0≤U[i],V[i]≤N−10 \le U[i], V[i] \le N - 1.
  • For all 0≤i≤M−10 \le i \le M - 1, U[i]≠V[i]U[i] \ne V[i].
  • For all 0≤i,j≤M−10 \le i, j \le M - 1 and i≠ji \ne j, (U[i],V[i])≠(U[j],V[j])(U[i], V[i]) \ne (U[j], V[j]) and (U[i],V[i])≠(V[j],U[j])(U[i], V[i]) \ne (V[j], U[j]).
  • It is guaranteed that you can travel from any island to any other island.

Subtasks

  1. (44 points) A=0,N≤100 000,M≤100 000A = 0, N \le 100\ 000, M \le 100\ 000.
  2. (44 points) M=N−1,N≤100 000,M≤100 000M = N - 1, N \le 100\ 000, M \le 100\ 000.
  3. (66 points) N≤300,M≤300N \le 300, M \le 300.
  4. (88 points) N≤4 000,M≤4 000N \le 4\ 000, M \le 4\ 000.
  5. (2222 points) N≤4 000,M≤1 000 000N \le 4\ 000, M \le 1\ 000\ 000.
  6. (1414 points) N≤100 000,M≤100 000N \le 100\ 000, M \le 100\ 000.
  7. (55 points) N≤300 000,M≤300 000N \le 300\ 000, M \le 300\ 000.
  8. (55 points) N≤500 000,M≤500 000N \le 500\ 000, M \le 500\ 000.
  9. (3232 points) No additional constraints.

Note: For subtask 99, the judge will use 15001500 ms out of the 45004500 ms time limit.

Translated by ChatGPT 5