Logistics
Time limit15sMemory limit1024 MB
For each store, count the valid production sequences from factory 1 that end at the store's factory with a level in its range, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Statement
Universal Manufacturing, a world-famous industrial company, has recently decided to expand its business. The company makes many kinds of products, from lamps to tractors, and handles the entire production process from raw materials to finished products inside its own factories.
The company has factories numbered , where factory 1 is the main factory. The factories are connected by roads, and it is known that every factory can be reached from the main factory by following these roads.
Factory has a level . The factory can produce components of level for other factories, as well as finished products of the same level. A factory may use components of any lower level as input, but the component or product it produces always has level .
The company has opened stores. Store is located at factory and can sell products with levels from to . Each product passes through a sequence of factories . Basic components are made at factory and sent to factory , where higher-level components are made from them. This continues until factory , where the final product is made and transported to the store. Basic components are always made at the main factory, so . Since each factory can only process components of a lower level, must hold for every .
Transport is the most expensive part of production. During the production of any single product, no factory may be visited more than once, either by a component or by the finished product, even if the product is not processed there. If no store can sell a product, that product is not sold at any store.
Input
The first line contains the number of factories (). The second line contains integers (), where means that there is a road between factory and factory . The exception is , which means there is no road. The third line contains integers (), the levels of the factories. It is known that and that all values are distinct. The fourth line contains the number of stores (). Each of the last lines contains three integers , , and (, ), meaning that store is located at factory and can sell products with levels from to .
Output
For each store , print on its own line the total number of factory sequences that correspond to all possible production processes for the products that store can sell. Since this number can be very large, print the remainder when it is divided by instead of the exact value.