Ski Resort
Time limit2sMemory limit512 MB
For each query, count the size-k sets of areas such that every favorite area has exactly one stocked area lying on every path from the top, and all chosen areas lie on paths to favorite areas.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, DFS, Combinatorics
- Solved
- No attempts yet
Problem
You own a well-known ski resort in the US, and you would like to increase your sales by stocking the snack stands at good locations across the resort.
The ski resort is built on a mountain. A ski lift takes a guest to the top of the mountain. From there, the guest can ski to a number of locations on the mountain.
There are areas on the mountain, labeled to . The top of the mountain is area . Areas are connected by ski runs that are strictly downhill. In other words, after leaving an area, a guest cannot return to it without taking the ski lift. Every area (including area ) has exactly one snack stand.
As the owner of the resort, you want to know how effectively you can distribute snacks among the snack stands to serve your guests better (and make more money). To this end, you run a survey and analyze the results with a number of independent queries. Each guest in the survey has a favorite snack and a list of favorite areas that they like to visit. You would like to know how best to stock your snack stands with their favorite snack.
Each query is a set of one guest's favorite areas and a number . Count the ways to stock this guest's favorite snack at exactly snack stands on the mountain so that the following conditions hold:
- For each of this guest's favorite areas, over all sequences of ski runs that reach that area from the top of the mountain, there must be exactly one snack stand with the guest's favorite snack. In other words, the guest must not have a choice of more than one snack stand where the snack is available. So among the areas that lie on at least one route from area to that area (both ends included), exactly one has a stocked stand, and every route from area to that area passes through it.
- Each of the snack stands stocked with this guest's favorite snack must lie on some sequence of ski runs from the top of the mountain to some area in the query set.
Input
The input consists of a single test case. Your program may be run multiple times on different inputs.
The first line contains three space-separated integers , , and , where () is the number of areas on the mountain, () is the number of runs, and () is the number of queries.
Each of the next lines contains two integers and (, ), meaning there is a ski run from area down to area . There is at most one run between any two areas. Every area can be reached from area by some chain of ski runs.
Each of the next lines is a sequence of space-separated integers that starts with and and continues with integers . Here () is the number of snack stands to stock with this guest's favorite snack, () is the number of areas in the query set, and the integers () are the labels of the areas in the query set. Within a query, no integer is repeated.
The sum of all over all queries does not exceed .
Output
Print integers, each on its own line, with no blank lines in between. Each integer is the number of ways to choose the snack stands to stock for the corresponding query, in the order the queries appear in the input. Two ways are different if an area is selected in one of them but not in the other.