Bitaro's Party
Time limit2sMemory limit512 MB
For each party at town T, find the largest number of canals any non-busy friend who can reach T must travel, given the busy friends listed for that party.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph
- Solved
- No attempts yet
Statement
There are towns of beavers numbered from to in descending order of their heights. No two towns have the same height. There are canals connecting two different towns, each flowing in one direction. The -th canal () flows from town to town . Every canal flows from a higher town to a lower town, so you cannot move against the flow of the canals.
Bitaro, a beaver, has friends, and one of them lives in each town.
Bitaro holds parties, inviting his friends. At the -th party (), friends are too busy to attend. The -th party is held in town . A friend who cannot travel from their town to town using only canals cannot attend either. All other friends come to the party.
Each friend travels to the party's town using canals. There may be several paths they can take. Bitaro's friends love canals, so each friend takes the path with the largest number of canals.
Bitaro wants to know how many canals the attendee who uses the most canals travels.
Given the town of each party and the list of busy friends for each of the parties, write a program that calculates how many canals the attendee who uses the most canals travels.
Input
The first line contains three space-separated integers , , and , the number of towns, canals, and parties.
Each of the next lines contains two integers and , meaning a canal flows from town to town .
Each of the next lines contains and , followed by integers . These mean the -th party is held in town , and the friends living in towns are busy.
Output
Print lines. The -th line contains the number of canals used by the attendee who uses the most canals for the -th party. If nobody can attend the -th party, print .
Constraints
- .
- .
- .
- for .
- for .
- for .
- for .
- for .
- .