시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 (추가 시간 없음) | 512 MB | 3 | 3 | 3 | 100.000% |
You are given a tree with $n$ vertices enumerated from $0$ to $n - 1$ inclusive. There is a number in each vertex. Numbers are from $0$ to $n - 1$ and are all distinct (i.e. they form a permutation). The number $x$ is in its place if it is in the $x$-th vertex.
You are allowed to swap two numbers at the endpoints of some edge of the tree. This costs you 0 if at least one of those numbers was in its place before the swap and 1 otherwise.
What is the minimum cost you have to pay to put all numbers in their places?
The first line contains a single integer $n$ ($2 \leq n \leq 10^5$), the number of vertices of the tree.
The second line contains $n - 1$ integers $p_i$ ($0 \leq p_i < i$). $i$-th of them describes an edge between vertices $p_i$ and $i$.
The third line contains $n$ integers $a_i$ ($0 \leq a_i < n$). $i$-th of them (in zero based indexing) is equal to the number which is initially in the $i$-th vertex. $a$ is a permutation.
Print a single integer --- the minimum cost you have to pay.
2 0 0 1
0
2 0 1 0
1
3 0 0 0 2 1
2