#C11929. Maximum Delay Time
Maximum Delay Time
Maximum Delay Time
You are given a directed graph representing a computer network. Each directed edge from u to v represents that a virus propagates from computer u to computer v in one time unit. Given a starting source computer S, the virus spreads along the edges. Your task is to compute the maximum delay time needed for the virus to reach any reachable computer in the network. Formally, if d(v) is the number of time units required for the virus to reach computer v from S, you need to determine \[ \max_{v \text{ is reachable }} d(v) \] If no other computer (except the source itself) is reached, output -1.
Note: It is guaranteed that the graph has no self-loops and may be disconnected.
inputFormat
The input is read from standard input (stdin
) and has the following format:
T N1 M1 u1 v1 ... uM1 vM1 S1 N2 M2 u1 v1 ... uM2 vM2 S2 ... NT MT ... (edges for test case T) ST
Here, T is the number of test cases. For each test case, the first line contains two integers N (number of computers) and M (number of directed edges). Each of the next M lines contains two integers u and v, indicating a directed edge from u to v. The last line of each test case contains an integer S, the source computer.
outputFormat
For each test case, output one integer on a separate line representing the maximum delay time for the virus to reach any computer starting from S. If the virus does not spread to any other computer, output -1.
## sample5
5 5
1 2
1 3
3 4
4 5
2 5
1
3 2
1 2
2 3
1
4 3
1 2
1 3
2 4
1
6 4
1 2
2 3
3 4
4 5
1
5 0
1
3
2
2
4
-1
</p>