14991 - Degree of Connection   

Description

You are required to write a C/C++ program to solve the following problem.

In the modern era, social networks tightly connect people from all over the world. There is a famous concept known as the "Six Degrees of Separation," which suggests that any two people on Earth are at most six social connections away from each other.

Assume you are currently working as a data analyst for a booming social media startup, "ConnectU". Your manager wants you to analyze the efficiency of information spread on the platform. Specifically, given a massive network of user friendships, you need to calculate the minimum number of human nodes (connections) required to link a specific "starting user" to a "target user".

In the ConnectU system, all friendship relations are mutual and carry the exact same weight (there is only a distinction between "friends" and "not friends", with no degrees of closeness). The most efficient way to find the shortest relationship path is to imagine a piece of news spreading: the news starts at the source, first transmits to all the direct friends of the starting user, then ripples outward layer by layer to the "friends of friends", continuing this outward expansion process until it finally reaches the target user.

Given a network containing N users and M friendship links, please find the minimum relationship distance between user S and user T.

  • The distance between a user and themselves is 0.
  • The distance between direct friends is 1.
  • The distance between a friend of a friend is 2, and so on.

Your program MAY use C/C++ standard library headers.

Input

The first line contains two integers N and M (1 <= N <= 100,000, 0 <= M <= 200,000), representing the total number of users and the number of friendship connections, respectively. Users are numbered from 0 to N-1.

The next M lines each contain two integers U and V, representing a mutual friendship between user U and user V (0 <= U, V < N and U != V).

The last line contains two integers S and T, representing the identifiers of the starting user and the target user, respectively.

(Note: The input test cases may contain duplicate friendship declarations, which should simply be treated as a single friendship relation in your processing.)

Output

Output a single integer on a single line representing the minimum distance between user S and user T. If there is no possible path connecting the two users (meaning they belong to completely isolated social circles), output -1.

Each line of output must be terminated by a newline character (\n).

Sample Input  Download

Sample Output  Download

Tags

yan_ds



Discuss