Kinship Distance
InterviewTime limit1sMemory limit128 MB
Given a family forest of parent-child edges, find the shortest kinship distance between two given people or output -1 if unconnected.
Problem
Family and relative relationships can be represented by a kinship distance. A parent and child have distance 1, and the distance increases by 1 for each parent-child relationship crossed.
Given several parent-child relationships, write a program that computes the kinship distance between two specified people. If the two people are not connected by these relationships, their distance cannot be computed.
Input
People are numbered consecutively from 1 through n (1 <= n <= 100).
The first line contains the total number of people n. The second line contains the numbers of two different people whose kinship distance must be computed. The third line contains m, the number of parent-child relationships.
Each of the next m lines contains two integers x and y, meaning that x is the parent of y.
Each person is given at most one parent.
Output
Print the kinship distance between the two people requested in the input. If the two people are not related through the given relationships, print -1.