Mancunian And Nancy Play A Game

3.7

26 votes
Graphs, Medium, Shortest Path Algorithm
Problem

Mancunian is in a committed relationship with his girlfriend Nancy. His idea of a date is for them to have a romantic candlelight dinner and then play graph games. This time they are using a graph of N nodes and E edges. Mancunian is located at vertex and wants to move to . Nancy wants to move from vertex to . Anyone can move at any time, till both have reached their respective destinations. But there is a catch. Nancy can't bear to be too far away from her true love, Mancunian and hence the shortest distance between them at any point in time should never exceed a specified parameter K.

You are given several possible pairs of , for Nancy. Count the number of such pairs which will make a valid game (where both Mancunian and Nancy can reach their respective destinations without violating the distance condition).

Input format

The first line contains three integers N, E and K denoting the number of vertices, number of edges in the graph and the maximum allowed distance between the two lovers respectively.
The following E lines contain two integers A and B, denoting an undirected edge between those two vertices. There will be no self-loops or multiple edges in the graph.
Next line contains two integers and denoting the source and destination for Mancunian.
Next line contains a single integer Q denoting the number of games.
Each of the next Q lines contains two integers and denoting the source and destination for Nancy, for that game.

Output format

Print a single integer, denoting the number of valid games.

Constraints

Sample Input
6 5 3
1 3
2 3
3 4
4 5
3 6
1 5
1
2 6
Sample Output
1
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The game may proceed in the following manner. Mancunian moves to vertex 3 and then Nancy also moves to 3. Mancunian moves to 4, and then 5. Nancy moves to vertex 6.

Editor Image

?