Speleology
InterviewTime limit3sMemory limit512 MB
Given a DAG where chambers are numbered top to bottom, find the maximum number of downward paths from chamber 1 to chamber n that use distinct first corridors and distinct last corridors.
- Level
Medium6 of 10
- Topics
- Graph, Dynamic programming, BFS, DFS
- Solved
- No attempts yet
Problem
A team of speleologists holds a training session in the Great Cave of the Byte Mountains. During the training, each speleologist explores one route from the Top Chamber to the Bottom Chamber. A speleologist may move downward only: every chamber along a route must lie strictly below the previous one.
In addition, each speleologist must leave the Top Chamber through a different corridor, and each of them must enter the Bottom Chamber through a different corridor. The corridors in between may be used by more than one speleologist. What is the maximum number of speleologists that can train at the same time?
Write a program that:
- reads the description of the cave from standard input,
- computes the maximum number of speleologists that can train simultaneously,
- writes the result to standard output.
Input
The first line contains one integer (), the number of chambers in the cave. The chambers are numbered from to from top to bottom: a chamber with a larger number lies lower (at a lower level), the Top Chamber is number , and the Bottom Chamber is number . Every corridor always leads from a lower-numbered chamber to a higher-numbered one, that is, downward.
Each of the next lines (lines through ) describes one chamber: line describes chamber . It begins with an integer (), the number of corridors leaving chamber , followed by the numbers of the chambers those corridors lead to. All of these numbers are greater than .
Output
Print a single integer on one line: the maximum number of speleologists that can train simultaneously.
Hint
