The Mysterious X Network
InterviewTime limit1sMemory limit128 MB
Given an undirected graph of N people, find the minimum number of intermediate people on a shortest path between two given people.
- Level
Medium4 of 10
- Topics
- Graph, BFS, Shortest path, Queue
- Solved
- No attempts yet
Problem
École polytechnique — nicknamed “X” — is famous for its network of camarades: former students of the school. Whenever a camarade needs something (money, a job, and so on), they can call on this network for help. To reach another camarade, not necessarily from the same class year, one can always find a chain of intermediate camarades who know one another. The camarade relationship is symmetric (if one knows the other, the other knows the first), and thanks to the network there is always a way to reach anybody.
Your task is to minimize the number of these intermediate camarades on the chain between two given people.
Input
The first line contains an integer (), the number of camarades, labeled from to .
Each of the next lines describes one camarade: it starts with that camarade's label , followed by the number of camarades that knows (), followed by the labels of those camarades. All integers on a line are separated by single spaces.
The last line contains two labels and (): is the camarade seeking help and is the camarade whose help is sought.
Output
Print three integers separated by single spaces: , , and the minimal number of intermediate camarades needed to reach from .
The intermediate camarades are those strictly between and on a shortest chain; if and know each other directly, this number is .