Under construction

Still being built. Things may change

Speleology

Interview

Time limit3sMemory limit512 MB

Summary
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 nn (2≤n≤2002 \le n \le 200), the number of chambers in the cave. The chambers are numbered from 11 to nn from top to bottom: a chamber with a larger number lies lower (at a lower level), the Top Chamber is number 11, and the Bottom Chamber is number nn. Every corridor always leads from a lower-numbered chamber to a higher-numbered one, that is, downward.

Each of the next n−1n-1 lines (lines 22 through nn) describes one chamber: line i+1i+1 describes chamber ii. It begins with an integer mm (0≤m≤n−i+10 \le m \le n-i+1), the number of corridors leaving chamber ii, followed by the numbers of the mm chambers those corridors lead to. All of these numbers are greater than ii.

Output

Print a single integer on one line: the maximum number of speleologists that can train simultaneously.

Hint

Examples3

  1. Example 1

    Input
    12
    4 3 4 2 5
    1 8
    2 9 7
    2 6 11
    1 8
    2 9 10
    2 10 11
    1 12
    2 10 12
    1 12
    1 12
    
    Expected output
    3
    
  2. Example 2

    Input
    2
    1 2
    
    Expected output
    1
    
  3. Example 3

    Input
    4
    2 2 3
    1 4
    1 4
    
    Expected output
    2