eolymp
bolt
Try our new interface for solving problems
Problems

Traitor

Traitor

According to an intelligence source, we’ve got a traitor in ACM Security Agency (ASA). ASA has a hierarchical structure where each agent has a manager and there are also some (at least one) top managers who are not managed by anyone. Our source doesn’t exactly know the traitor, but he has a list of suspects. Therefore, all we know is that there is exactly one traitor in our agency and we have a list of suspects. In order to find that traitor, we want to assign a watcher for each suspect, satisfying the following three conditions: \begin{enumerate} \item Two suspects cannot watch each other. \item Each suspect should be watched by either his manager or one of his direct employees. \item Nobody can watch more than one suspect. \end{enumerate} If we want to satisfy all above conditions, it may be impossible to watch all suspects. Therefore, you should write a program that gets the structure of ASA and the list of suspects as the input and finds the maximum number of suspects for whom the watcher assignment is possible. In the following figure that illustrates the organizational structure of ASA with two top managers and eleven agents, the suspects are indicated with gray color. One watcher assignment covering \textbf{7} out of \textbf{8} suspects is possible in this case which is shown by arrows. An arrow from agent \textbf{x} to agent \textbf{y}, means agent \textbf{x }is supposed to watch agent \textbf{y}. It can be shown that in this example there is no watcher assignment covering all suspects. \includegraphics{https://static.e-olymp.com/content/b9/b9d79776b82efbc8c8c007da7f5a00f3ada1e587.jpg} \InputFile There are multiple test cases in the input. The first line of each test case contains two integers \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{10000}) specifying the number of agents, and \textbf{k} (\textbf{1} ≤ \textbf{k} ≤ \textbf{n}) specifying the number of the suspected agents. The agents are numbered from \textbf{1} to \textbf{n}. On the second line there are \textbf{n} space-separated integers, where the \textbf{i}^th number is the number of agent who is the manger of the agent \textbf{i}. A zero means the agent \textbf{i} is a top manager. On the third line there are \textbf{k }positive integers \textbf{s_1}, \textbf{s_2}, ..., \textbf{s_k}, indicating the numbers of the suspected agents. The input terminates with "\textbf{0 0}" which should not be processed. \OutputFile For each test case, output in a line the maximum number of suspects for whom the watcher assignment is possible.
Time limit 1 second
Memory limit 64 MiB
Input example #1
11 8
3 4 4 0 6 8 9 11 11 11 0
3 2 8 9 6 5 7 11
2 2
0 1
1 2
0 0
Output example #1
7
1
Source 38th ACM International Collegiate Programming Contest, 2013–2014 Asia Region, Tehran Site, Sharif University of Technology, 19–20 December 2013