Botanist Somhed regularly takes groups of students to one of Thailand's largest tropical gardens. The landscape of this garden is composed of
Somhed loves beautiful tropical plants. Therefore, from any fountain he and his students will take the most beautiful trail leaving that fountain, unless it is the most recent trail taken and there is an alternative. In that case, they will take the second most beautiful trail instead. Of course, if there is no alternative, they will walk back, using the same trail for the second time. Note that since Somhed is a professional botanist, no two trails are considered equally beautiful for him.
His students are not very interested in the plants. However, they would love to have lunch at a premium restaurant located beside fountain number
- each group can start at any fountain;
- the successive trails must be chosen in the way described above; and
- each group must finish at fountain number
after traversing exactly trails.
Note that they may pass fountain number
Your task
Given the information on the fountains and the trails, you have to find the answers for
Write a program that takes the following parameters:
– the number of fountains. The fountains are numbered through . – the number of trails. The trails are numbered through . The trails will be given in decreasing order of beauty: for , trail is more beautiful than trail . – the fountain at which the premium restaurant is located. – a two-dimensional array representing the trails. For , trail connects the fountains and . Recall that each trail joins a pair of distinct fountains, and no two trails join the same pair of fountains. – the number of groups of students. – a one-dimensional array of integers containing the values of . For , is the number of trails that the -th group will take.
For 0
.
Examples
Example 1
Consider the case shown in Figure 1, where
Note that trails are listed in decreasing order of beauty. That is, trail
, and .
The first route starts at fountain
Thus, the program should output 2
.
Example 2
Consider the case shown in Figure 2, where
For the first group, there is only one valid route that reaches fountain
For the second group, there are two valid routes that reach fountain
Therefore, a correct program should first output 1
to report the answer for the first group, and then output 2
to report the answer for the second group.
Input Specification
- Line
: , , and - Lines
to : description of the trails; i.e., line contains and , separated by a space, for . - Line
: . - Line
: array as a sequence of space-separated integers.
Output Specification
One line containing
Sample Input 1
5 5 2
1 0
1 2
3 2
1 3
4 2
2
3 1
Sample Output 1
1 2
Sample Input 2
6 6 0
1 2
0 1
0 3
3 4
4 5
1 5
1
3
Sample Output 2
2
Subtasks
Subtask 1 (49 points)
- each element of
is an integer between and , inclusive.
Subtask 2 (20 points)
- each element of
is an integer between and , inclusive.
Subtask 3 (31 points)
- each element of
is an integer between and , inclusive.
Comments