#P2321. [HNOI2006] 潘多拉的宝盒

    ID: 1290 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>搜索2006各省省选湖南广度优先搜索,BFSTarjan

[HNOI2006] 潘多拉的宝盒

Description

Legend has it that there is a magical Pandora's box. Whoever opens it will gain happiness, wealth, and love. However, only after truly opening it did people realize that disaster and misfortune come along with it.

In fact, when Pandora made this box, she set up some spells to seal away disaster and misfortune. Yet, only in today’s highly advanced scientific age do people have hope of understanding these spells. So for thousands of years, people had no choice but to endure various diseases and the pain of death.

However, humanity’s fate has since changed. After decades of research, the NOI organization has finally uncovered the principles behind Pandora’s spells.

The spells are produced by a machine called a spell machine. In modern terms, a spell machine is essentially a binary generator. The binary string it generates (this string is called a spell source), once encrypted, becomes a spell. The binary generator has the following structure:

It consists of nn components, numbered from 00 to n1n - 1. At any moment, there is exactly one signal, and it stays at some component. A signal is a binary string. Initially, an empty-string signal stays at component 00. At some moment, if a signal ss is staying at component ii, then component ii may append a 0 to the end of the signal and pass it to component pi,0p_{i,0}, or append a 1 and pass it to component pi,1p_{i,1}. That is, at the next moment, either a signal S0 (meaning the string S with a 0 appended) stays at component pi,0p_{i,0}, or a signal S1 stays at component pi,1p_{i,1}.

Some components can output the signal staying on them, and the output signal becomes a spell source. Such components are called spell-source output units.

It is not hard to see that some spell sources can be produced by a given spell machine, while others cannot.

For example, the spell machine in the figure below can produce spell sources such as 1, 11, 111, 1111, …, but cannot produce 0, 10, 101, etc.

On this box, there are kk spell machines, labeled from 00 to k1k - 1. It may happen that every spell source producible by spell machine ii can also be produced by spell machine jj. In that case, we say machine jj is an upgrade of machine ii. One way to measure the complexity of this instance is to consider the maximum number of upgrades of any spell machine on the box. That is, find a longest upgrade sequence a1,a2,,ata_1, a_2, \dots, a_t. This upgrade sequence must satisfy: all machine indices in the sequence are distinct, each is an integer between 00 and k1k - 1 (inclusive), machine a2a_2 is an upgrade of machine a1a_1, machine a3a_3 is an upgrade of machine a2a_2, …, and machine ata_t is an upgrade of machine at1a_{t-1}.

Do you want to stay away from disaster and misfortune? Do you want to bask in the sunshine of happiness from now on? Please open your Pandora’s box. But before opening it, you need to compute the longest upgrade sequence on the box.

Input Format

The first line contains a positive integer kk (1k50)(1 \le k \le 50), the number of spell machines on the box.

Then follow kk parts, one for each machine:

  • The first line of each part contains two positive integers n,mn, m (1n,m50)(1 \le n, m \le 50), the number of components in this spell machine and the number of spell-source output units, respectively.
  • The next line contains mm integers, the indices of the mm spell-source output units.
  • The next nn lines describe transitions. In the ii-th line, there are two integers pi,0,pi,1p_{i,0}, p_{i,1} between 00 and n1n - 1 (inclusive).

Output Format

Output one line with a single integer tt, the length of the longest upgrade sequence.

4
1  1
0
0  0
2  1
0
1  1
0  0
3  1
0
1  1
2  2
0  0
4  1
0
1  1
2  2
3  3
0  0
3
3
1  1
0
0  0
3  1
0
0  1
2  0
1  2
9  1
0
0  1
2  3
4  5
6  7
8  0
1  2
3  4
5  6
7  8
3

Hint

For the first sample’s box, there are 44 spell machines. It is not hard to see that for the ii-th (i=0,1,2,3i = 0, 1, 2, 3) spell machine, all producible spell sources have lengths that are multiples of (i+1)(i + 1).

For the second sample’s box, there are 33 spell machines: machine 00 can produce all spell sources; machine 11 can produce all spell sources whose values, when interpreted as binary, are multiples of 33; machine 22 can produce all spell sources whose values, when interpreted as binary, are multiples of 99.

Translated by ChatGPT 5