#P8341. Recollection Rounds
Recollection Rounds
Recollection Rounds
In their youth, they formulated n problems, numbered from \(1\) to \(n\). Each problem (except problem \(1\)) is derived from a more basic one, so the problems form a tree with problem \(1\) as the root. Two problems are said to be related if they are adjacent in the tree.
They later conducted m studies. The \(i\)th study starts with a relatively basic problem \(s_i\) and, through modifications and generalizations, eventually leads to problem \(t_i\). It is guaranteed that \(s_i \neq t_i\) and also that \(s_i\) is an ancestor of \(t_i\) (i.e. \(s_i\) lies on the unique path from the root to \(t_i\)). Note that even if the studied problem pair is identical, the outcome may differ. In other words, it is possible that for \(i \neq j\) we have \((s_i,t_i)=(s_j,t_j)\).
Now they recall the problems in separate rounds. In each round, they may start by recalling any one of the \(n\) problems. Then, as long as there exists a problem related to the current one and not yet recalled in this round, they can switch their thought to any one such related problem and recall it. They may continue switching at will, and may stop recalling at any point. (Each round is independent so a problem may be recalled in more than one round.)
A study \(i\) is said to be recalled if in some round both problem \(s_i\) and problem \(t_i\) are recalled in the same round.
For example, suppose \(n=5\) with tree-edges as follows: problem \(1\) is related to problems \(2\) and \(3\), and problem \(3\) is related to problems \(4\) and \(5\). One possible round is: start at \(2\), switch to \(1\), then to \(3\), and finally to \(5\) (and then stop). If \(m=4\) with \((s_1,t_1)=(1,2)\), \((s_2,t_2)=(1,4)\), and \((s_3,t_3)=(s_4,t_4)=(3,5)\), then that round recalls studies \(1\), \(3\) and \(4\) (but not study \(2\)).
Your task is to determine the minimum number of recollection rounds needed so that all m studies are recalled at least once, assuming the starting problem and the choices of switching can be arbitrary.
Note on Recollection Rounds: A round is equivalent to choosing a simple path in the tree (i.e. a path which does not repeat vertices). A study \((s,t)\) (with \(s\) an ancestor of \(t\)) is recalled in a round if the unique simple path connecting \(s\) and \(t\) is a subset of the recalled path.
inputFormat
The first line contains two integers \(n\) and \(m\) \((2 \le n, 1 \le m)\), representing the number of problems and the number of studies respectively.
The next \(n-1\) lines describe the tree. For \(i=2,\dots,n\), the \(i\)th line contains an integer \(p_i\) \((1 \le p_i < i)\) denoting that problem \(p_i\) is the parent of problem \(i\); thus, problem \(1\) is the root.
The following \(m\) lines each contain two integers \(s_i\) and \(t_i\) \((1 \le s_i < t_i \le n)\), indicating that in the \(i\)th study, problem \(s_i\) (which is an ancestor of \(t_i\)) is developed into problem \(t_i\).
outputFormat
Output a single integer: the minimum number of recollection rounds required so that every study is recalled at least once.
sample
5 4
1
1
3
3
1 2
1 4
3 5
3 5
2