Canadian Computing Olympiad: 2021 Day 2, Problem 2
There are
- Each town except for
is adjacent to another town given earlier in the order. - The towns are given in non-decreasing order of distance to
. Here, distance means the minimum number of roads that need to be traversed to reach a town.
However, someone mistakenly did a bread first search. They found the ordering
To cover up this embarrassment, the government would like to build new roads
such that
Input Specification
The first line contains the two integers
The
For 5 of the 25 available marks,
For an additional 6 of the 25 marks available,
Output Specification
On a single line, output the minimum number of roads that must be constructed.
Sample Input 1
2 0
Sample Output 1
1
Explanation for Sample Output 1
For 1, 2 to be a breadth first search ordering, a road between towns 1 and 2 must be built.
Sample Input 2
6 3
1 3
2 6
4 5
Sample Output 2
2
Explanation for Sample Output 2
By building a road between 1 and 2 and a road between 1 and 4, the sequence of
distances becomes
Comments