Botanist Somhed regularly takes groups of students to one of Thailand's largest tropical gardens. The landscape of this garden is composed of fountains (numbered ) and trails. Each trail connects a different pair of distinct fountains, and can be traveled in either direction. There is at least one trail leaving each fountain. These trails feature beautiful botanical collections that Somhed would like to see. Each group can start their trip at any fountain.
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 . Somhed knows that his students will become hungry after taking exactly trails, where could be different for each group of students. Somhed wonders how many different routes he could choose for each group, given that:
- 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 earlier on their route, although they still need to finish their route at fountain number .
Your task
Given the information on the fountains and the trails, you have to find the answers for groups of students; that is, values of .
Write a procedure count_routes(N,M,P,R,Q,G)
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 , your procedure must find the number of possible routes with exactly trails that group could possibly take to reach fountain . For each group , your procedure should call the procedure answer(X)
to report that the number of routes is . The answers must be given in the same order as the groups. If there are no valid routes, your procedure must call answer(0)
.
Examples
Example 1
Consider the case shown in Figure 1, where , , , , , and .
Note that trails are listed in decreasing order of beauty. That is, trail is the most beautiful one, trail is the second most beautiful one, and so on. There are only two possible valid routes that follow 3 trails:
- , and
- .
The first route starts at fountain . The most beautiful trail from here leads to fountain . At fountain , the group has no choice, they must return using the same trail. Back at fountain , the group will now avoid trail and choose trail instead. This trail does indeed bring them to the fountain .
Thus, the procedure should call answer(2)
.
Example 2
Consider the case shown in Figure 2, where , , , , , , and .
For the first group, there is only one valid route that reaches fountain after following trails: .
For the second group, there are two valid routes that reach fountain after following trail: , and .
Therefore, the correct implementation of count_routes
should first call answer(1)
to report the answer for the first group, and then call answer(2)
to report the answer for the second group.
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.
Implementation details
Interface (API)
To be implemented by contestant:
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]);
To be called by contestant:
void answer(int X);
Comments