#SRD0000102. Bữa tiệc của Bitaro (Bitaro's Party)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bữa tiệc của Bitaro (Bitaro's Party)
Bitaro's Party
Source: JOI
Version: Phuoc Hung OJ Extended
Problem Statement
There are towns numbered through in descending order of altitude. There are directed canals; a canal flows from a higher town to a lower town, so . Travel is possible only along the direction of a canal. No directed canal occurs twice.
Bitaro holds parties. Party takes place in town . Exactly friends are busy, living in towns . There is one friend in each town. A busy friend cannot be a starting attendee, but another friend's route may pass through that friend's town.
Every non-busy friend who can reach using canals attends. Each such friend chooses a path with the largest possible number of canals to (a friend already at may use zero canals). Report the maximum number of canals used by any attendee, or -1 if nobody can attend.
Input
- The first line contains .
- Each of the next lines contains , a directed canal from to .
- Each of the next lines starts with , followed by exactly town indices in strictly increasing order. If , no additional indices appear on that line.
Output
For each party, output the largest number of canals used by an attendee, or -1 if there are no attendees. Print one integer per line.
Subtasks
- Subtask 1 (7 points): , , ; with no repeated edges; , , strictly increasing ; .
- Subtask 2 (7 points): , , ; with no repeated edges; , , strictly increasing ; .
- Subtask 3 (86 points): , , ; with no repeated edges; , , strictly increasing ; .
Examples
Input
5 6 3
1 2
2 4
3 4
1 3
3 5
4 5
4 1 1
5 2 2 3
2 3 1 4 5
Output
1
3
0
Explanation
For the party at town 4, the friend from town 1 is busy. The longest valid arrival path, from town 2 or 3, uses one canal. For the party at town 5, the friends from towns 2 and 3 are busy, while the friend from town 1 can follow (three canals). For the party at town 2, towns 1, 4 and 5 are banned; only the friend living at town 2 can attend, using zero canals.