#K54952. Minimum Roads to Travel

    ID: 29867 Type: Default 1000ms 256MiB

Minimum Roads to Travel

Minimum Roads to Travel

You are given a network of n cities connected by n-1 roads, forming a tree structure. In this problem, you must determine the minimum number of roads required to travel from a given initial city to a destination city.

The input guarantees that the cities are connected in such a way that there is exactly one unique path between any two cities. Formally, if the cities are numbered from 1 to n, the roads connect the cities such that the network is a tree, i.e. it has exactly $n-1$ edges.

Example:

  • For n = 5 and the following roads: (1, 2), (1, 3), (2, 4), (3, 5), the minimum number of roads to travel from city 1 to city 4 is 2.
  • For n = 6 and roads: (1, 2), (1, 3), (2, 4), (2, 5), (3, 6), the minimum number of roads to travel from city 4 to city 3 is 3.

Your task is to implement an algorithm (using breadth-first search) that finds the shortest path between the two given cities.

inputFormat

The input is read from stdin and has the following format:

  1. The first line contains an integer n (2n2 \leq n), the number of cities.
  2. The next n-1 lines each contain two space-separated integers u and v, indicating that there is a road between city u and city v.
  3. The final line contains two space-separated integers, the initial city and the destination city.

It is guaranteed that the given roads connect all the cities forming a tree.

outputFormat

Output to stdout a single integer: the minimum number of roads that must be traversed to go from the initial city to the destination city.## sample

5
1 2
1 3
2 4
3 5
1 4
2