CF902B.Coloring a Tree

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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).

你被给定一棵包含 nn 个顶点的有根树。顶点编号为 11 到 nn,其中根节点为顶点 11。

每个顶点都有一个颜色,记顶点 vv 的颜色为 cvc_v。初始时 cv=0c_v = 0。

你需要通过最少的操作次数,将整棵树染成目标颜色。每次操作中,你可以选择一个顶点 vv 和一种颜色 xx,并将 vv 的子树(包括 vv 自身)中所有顶点染成颜色 xx。换言之,对每个顶点 uu,若从根到 uu 的路径经过 vv,则令 cu=xc_u = x。

题目保证:最终每个顶点的颜色均不为 00。

你可通过如下链接了解有根树的定义:https://en.wikipedia.org/wiki/Tree_(graph_theory)。

输入格式

The first line contains a single integer n (2 ≤ n ≤ 104) — 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.

第一行包含一个整数 nn(2≤n≤1042 \leq n \leq 10^4)——树中顶点的数量。

第二行包含 n−1n-1 个整数 p2, p3, …, pnp_2,\ p_3,\ \dots,\ p_n(1≤pi<i1 \leq p_i < i),其中 pip_i 表示顶点 ii 与顶点 pip_i 之间存在一条边。

第三行包含 nn 个整数 c1, c2, …, cnc_1,\ c_2,\ \dots,\ c_n(1≤ci≤n1 \leq c_i \leq n),其中 cic_i 表示第 ii 个顶点应被染成的颜色。

保证所给图是一棵树。

输出格式

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 1 into color 2 (numbers are colors):

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

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

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 1 into color 3 (numbers are colors):

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

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

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

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

第一个样例中的树如图所示(数字为顶点的编号):

第一步:我们将顶点 1 的子树中所有顶点染成颜色 2(数字表示颜色):

第二步:我们将顶点 5 的子树中所有顶点染成颜色 1:

第三步:我们将顶点 2 的子树中所有顶点染成颜色 1:

第二个样例中的树如图所示(数字为顶点的编号):

第一步:我们将顶点 1 的子树中所有顶点染成颜色 3(数字表示颜色):

第二步:我们将顶点 3 的子树中所有顶点染成颜色 1:

第三步:我们将顶点 6 的子树中所有顶点染成颜色 2:

第四步:我们将顶点 4 的子树中所有顶点染成颜色 1:

第五步:我们将顶点 7 的子树中所有顶点染成颜色 3:

输入解题思路,AI测评打分。不知道怎么写?

首页