#C5764. Maximum Satisfied Employees

    ID: 49449 Type: Default 1000ms 256MiB

Maximum Satisfied Employees

Maximum Satisfied Employees

You are given n employees numbered from 0 to n-1 and a seating arrangement represented as a permutation of these numbers. The seating arrangement is given in an array, where the i-th position represents the employee sitting in that seat.

Each employee i has a list of desired neighbors, given as a list of employee numbers. An employee is considered satisfied if at least one of his adjacent neighbors (to the left or right in the seating order) is in his list of desired neighbors.

You are allowed to perform at most one swap between any two employees in the seating arrangement. Your task is to determine the maximum number of satisfied employees that can be achieved after at most one swap.

The satisfaction condition for an employee i can be expressed using the following formula:

$S(i)=\begin{cases}1,& \text{if } (\exists\, j\text{ adjacent to }i)\;\text{such that } j \in D(i)\\0,& \text{otherwise} \end{cases}$

where D(i) denotes the set of desired neighbors for employee i.

inputFormat

The input is given from stdin in the following format:

n
s<sub>0</sub> s<sub>1</sub> ... s<sub>n-1</sub>
k0 a0 a1 ... a(k0-1)
k1 b0 b1 ... b(k1-1)
...
kn-1 z0 z1 ... z(kn-1-1)

Here:

  • n is the number of employees.
  • The second line is the seating arrangement with n space-separated integers representing the employee numbers.
  • The next n lines each describe the desired neighbors for employee 0 through employee n-1 respectively. Each of these lines starts with an integer k (the number of desired neighbors) followed by k space separated integers.

outputFormat

Output a single integer to stdout indicating the maximum number of satisfied employees after performing at most one swap.

## sample
4
0 2 3 1
2 1 3
1 0
2 1 2
1 0
3