Problem P
Ski Resort
As an enterprising owner of a world-renowned ski resort in the US, you would like to increase your sales by stocking snack stands at key locations throughout your estate.
The ski resort is built on a mountain. A ski lift can take a guest to the top of the mountain. From there they can ski to a number of locations throughout the mountain.
There are
As the owner of the resort, you want to know how effectively you can distribute snacks among snack stands to better serve your guests (and make more money). To this end you would like to run a survey, and analyze the result 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 to best stock your snack stands with their favorite snack.
Each query is a set of a guest’s favorite areas, and a
number
-
For each of this guest’s favorite areas, over all sequences of ski runs to 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, they must not have a choice of more than one snack stand where their snack is available.)
-
Each of the
snack stands stocked with this guest’s favorite snack must be on some sequence of ski runs from the top of the mountain to some area in the query set.
Input
Each input will consist of a single test case. Note that
your program may be run multiple times on different inputs. The
first line of input will contain three space-separated integers
The next
The next
The sum of all
Output
Output
Sample Input 1 | Sample Output 1 |
---|---|
4 4 4 1 2 1 3 2 4 3 4 1 1 4 2 1 4 1 1 3 2 2 3 2 |
2 0 2 1 |
Sample Input 2 | Sample Output 2 |
---|---|
8 10 4 1 2 2 3 1 3 3 6 6 8 2 4 2 5 4 7 5 7 7 8 2 3 4 5 6 2 2 6 8 1 1 6 1 1 8 |
0 0 3 2 |
Sample Input 3 | Sample Output 3 |
---|---|
8 9 4 1 2 1 3 3 6 6 8 2 4 2 5 4 7 5 7 7 8 2 3 4 5 6 2 2 6 8 1 1 6 1 1 8 |
2 0 3 2 |