Hide

Problem F
Spare Key

Submissions to this problem will only be marked as accepted if they receive at least a score of 100

Tyko’s neighbors are exceptionally clumsy. Every time they leave their house, they end up losing their keys. Luckily, they have all given one or several spare keys to other people in the neighborhood, and the spare key is kept inside the holders’ house, so if that person is home they can open the door with the spare key. This might initially seem like a good solution, but remember that the holder of the spare key is also clumsy, and might lose the keys to their own house in an instant!

Say that Alice has given her spare key to Bob, who has given his spare key to Charlie, who has given his spare key to Alice, creating a cycle. All of them will go on their own errands today, with Alice leaving the house first, then Bob and finally Charlie. When Alice goes out and inevitably loses her keys, she can call Bob for help. When Bob goes out, he loses his own keys and can therefore not go inside to get Alice’s spare key, and will have to ask Charlie for help. When Charlie goes out, the three of them are all unable to get inside because they need the spare key in their friend’s locked house.

Tyko has used his networking skills to find out who each of his neighbors has given their spare keys to. Tyko also happens to know that all of his neighbors will go on errands today (independently of each other) and the order in which they will leave their houses. When someone goes on an errand, they lose their keys immediately. Now Tyko needs your help to find the number of people who are locked out after each errand.

It should be noted when going on an errand, a person loses their key. However, when retrieving their spare key, they do not lose the spare key.

Input

The first line of input contains an integer $N$ ($1 \leq N \leq 500000$), the number of neighbors.

Then follow $N$ lines. Each line begins with an integer $m_i$ ($1 \leq m_i$), the number of spare keys that person $i$ has given out. The rest of the line consists of $m_i$ integers $s_{ij}$, meaning that person $i$ gave their $j$th spare key to person $s_{ij}$. Noone will give several keys to the same person.

Then follow $N$ lines, each containing an integer $e_i$ ($1 \leq e_i \leq N$), meaning that neighbor $e_i$ has now exited their house to go on an errand. Note that the neighbors leave their houses one after another.

Let $M$ be the total number of spare keys that are given out, i.e. $M = \sum _{i=1}^N m_i$. It is guaranteed that $M \leq 500000$.

Output

A line of $N$ integers. For each $1 \leq i \leq N$, output the number of neighbors who are permanently locked out of their homes when all $e_k$, $1 \leq k \leq i$ have left their house.

Scoring

Group

Points

Constraints

$1$

$15$

$N, M \leq 1000$

$2$

$85$

No further constraints

Sample Input 1 Sample Output 1
3
1 2
1 3
1 1
1
2
3
0 0 3
Sample Input 2 Sample Output 2
3
1 2
1 1
1 1
1
2
3
0 2 3

Please log in to submit a solution to this problem

Log in