#P7832. [CCO 2021] Bread First Search

    ID: 8907 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP线段树2021CCO(加拿大)广度优先搜索 BFS

[CCO 2021] Bread First Search

Problem Description

This country has nn cities and mm undirected roads.

A person wants to travel around the country, but they are very picky. They require the travel route to be a BFS (Bread First Search) order: it must visit every city exactly once, and satisfy the following conditions:

  • The first city can be chosen arbitrarily.
  • Except for the first city, before each city is visited, at least one adjacent city has already been visited.
  • The distances from each city to the first city are monotonically non-decreasing. The distance between two cities is defined as the minimum number of edges on a path between them.

This person is also obsessive, and insists on visiting the cities exactly once in the order of labels 1n1 \sim n. To make this travel route satisfy all the requirements above, the government needs to build some new roads. What is the minimum number of new roads needed?

Input Format

The first line contains two integers n,mn, m.

The next mm lines each contain two integers a,ba, b, representing an undirected road in this country.

Output Format

Output one line with one integer, representing the required value.

2 0
1
6 3
1 3
2 6
4 5
2

Hint

Explanation for Sample #2

One valid way is to build a road between cities 11 and 22, and a road between cities 11 and 44. Then the distances from city 11 to cities 161 \sim 6 are 0,1,1,1,2,20, 1, 1, 1, 2, 2, respectively.

Constraints

For 1132\frac{11}{32} of the testdata, 1n2001 \leq n \leq 200.

For 58\frac{5}{8} of the testdata, 1n5×1031 \leq n \leq 5 \times 10^3.

For 100%100\% of the testdata, 1n2×1051 \leq n \leq 2 \times 10^5, $0 \leq m \leq \min(2 \times 10^5, \frac{n(n - 1)}{2})$, 1a,bn1 \leq a, b \leq n. It is guaranteed that there are no multiple edges or self-loops.

Source

CCO2021 D2T2

Translated by ChatGPT 5