Codeforces Round 453 (Div. 2)
B
Coloring a Tree
Codeforces
1200
原题
← AC →
翻译中…

You are given a rooted tree with $*n*$ vertices. The vertices are numbered from $1$ to $*n*$, the root is the vertex number $1$.

翻译中…

Each vertex has a color, let's denote the color of vertex $*v*$ by $*c*_{*v*}$. Initially $*c*_{*v*} = 0$.

翻译中…

You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex $*v*$ and a color $*x*$, and then color all vectices in the subtree of $*v*$ (including $*v*$ itself) in color $*x*$. In other words, for every vertex $*u*$, such that the path from root to $*u*$ passes through $*v*$, set $*c*_{*u*} = *x*$.

翻译中…

It is guaranteed that you have to color each vertex in a color different from $0$.

翻译中…

You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory).

翻译中…

Input

翻译中…

The first line contains a single integer $*n*$ ($2 ≤ *n* ≤ 10^{4}$) — the number of vertices in the tree.

翻译中…

The second line contains $*n* - 1$ integers $*p*_{2}, *p*_{3}, ..., *p*_{*n*}$ ($1 ≤ *p*_{*i*} < *i*$), where $*p*_{*i*}$ means that there is an edge between vertices $*i*$ and $*p*_{*i*}$.

翻译中…

The third line contains $*n*$ integers $*c*_{1}, *c*_{2}, ..., *c*_{*n*}$ ($1 ≤ *c*_{*i*} ≤ *n*$), where $*c*_{*i*}$ is the color you should color the $*i*$-th vertex into.

翻译中…

It is guaranteed that the given graph is a tree.

翻译中…

Output

翻译中…

Print a single integer — the minimum number of steps you have to perform to color the tree into given colors.

翻译中…

Note

翻译中…

The tree from the first sample is shown on the picture (numbers are vetices' indices):

翻译中…

image

翻译中…

On first step we color all vertices in the subtree of vertex $1$ into color $2$ (numbers are colors):

翻译中…

image

翻译中…

On seond step we color all vertices in the subtree of vertex $5$ into color $1$:

翻译中…

image

翻译中…

On third step we color all vertices in the subtree of vertex $2$ into color $1$:

翻译中…

image

翻译中…

The tree from the second sample is shown on the picture (numbers are vetices' indices):

翻译中…

image

翻译中…

On first step we color all vertices in the subtree of vertex $1$ into color $3$ (numbers are colors):

翻译中…

image

翻译中…

On second step we color all vertices in the subtree of vertex $3$ into color $1$:

翻译中…

image

翻译中…

On third step we color all vertices in the subtree of vertex $6$ into color $2$:

翻译中…

image

翻译中…

On fourth step we color all vertices in the subtree of vertex $4$ into color $1$:

翻译中…

image

翻译中…

On fith step we color all vertices in the subtree of vertex $7$ into color $3$:

翻译中…

image

翻译中…

样例

Input #1
6
1 2 2 1 5
2 1 1 1 1 1
Output #1
3
InputCopy OutputCopy
Input #2
7
1 1 2 3 1 4
3 3 1 1 1 2 3
Output #2
5
InputCopy OutputCopy
题目信息
题号
902B
时间限制
1 s
内存限制
256 MB
标签
DFS 及类似
并查集
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。