CF902B.Coloring a Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a rooted tree with nn vertices. The vertices are numbered from 11 to nn , the root is the vertex number 11 .

Each vertex has a color, let's denote the color of vertex vv by cvc_{v} . Initially cv=0c_{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 vv and a color xx , and then color all vectices in the subtree of vv (including vv itself) in color xx . In other words, for every vertex uu , such that the path from root to uu passes through vv , set cu=xc_{u}=x .

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

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

输入格式

The first line contains a single integer nn ( 2<=n<=1042<=n<=10^{4} ) — the number of vertices in the tree.

The second line contains n1n-1 integers p2,p3,...,pnp_{2},p_{3},...,p_{n} ( 1<=pi<i1<=p_{i}<i ), where pip_{i} means that there is an edge between vertices ii and pip_{i} .

The third line contains nn integers c1,c2,...,cnc_{1},c_{2},...,c_{n} ( 1<=ci<=n1<=c_{i}<=n ), where cic_{i} is the color you should color the ii -th vertex into.

It is guaranteed that the given graph is a tree.

输出格式

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

输入输出样例

  • 输入#1

    6
    1 2 2 1 5
    2 1 1 1 1 1
    

    输出#1

    3
    
  • 输入#2

    7
    1 1 2 3 1 4
    3 3 1 1 1 2 3
    

    输出#2

    5
    

说明/提示

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

On first step we color all vertices in the subtree of vertex 11 into color 22 (numbers are colors):

On seond step we color all vertices in the subtree of vertex 55 into color 11 :

On third step we color all vertices in the subtree of vertex 22 into color 11 :

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

On first step we color all vertices in the subtree of vertex 11 into color 33 (numbers are colors):

On second step we color all vertices in the subtree of vertex 33 into color 11 :

On third step we color all vertices in the subtree of vertex 66 into color 22 :

On fourth step we color all vertices in the subtree of vertex 44 into color 11 :

On fith step we color all vertices in the subtree of vertex 77 into color 33 :

首页