Coloring a Tree(耐心翻译+思维)

Posted wkfvawl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Coloring a Tree(耐心翻译+思维)相关的知识,希望对你有一定的参考价值。

Description

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 cv. Initially cv = 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 cu = 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 ≤ 104) — the number of vertices in the tree.

The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi.

The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci 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.

Sample Input

Input
6
1 2 2 1 5
2 1 1 1 1 1
Output
3
Input
7
1 1 2 3 1 4
3 3 1 1 1 2 3
Output
5

Hint

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:

技术分享图片

 

   题目意思: 给你n个点,代表这一棵树有n个节点。第二行内容是建树的关系,(开始我一直看不明白是如何建树的,后来翻译给队友,他帮我指了)

   从第二个节点开始的节点和父节点(上一个节点)相连,

例如:1 2 2 1 5
代表:节点2和节点1相连,节点3和节点2相连,节点4和节点2相连,节点5和节点1相连,节点6和节点5相连。

第三行内容是需要将各个点涂成的颜色,给这个树涂色,有这么一条原则就是给某一节点涂色,以其为根节点的子树也将变为相应的颜色,我们可以成为一种颜料的溢出,问你最终需要

最少需要涂多少次颜色就可以满足题目要求。

解题思路:我们可以这样来思考,因为最后需要使所有的点都涂成要求的颜色,一定是按照从根节点到叶子节点遍历的涂色,但所有的点都遍历会造成浪费,我们只需要找出需要涂的点即可,
那么哪些点需要涂呢?我们发现只有那些最后要求的其父亲节点和本身不同色的需要涂色,因为需要向下改变自身颜色,那么只需要统计这样点的个数即可。


 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int pr[10010];
 6 int a[10010];
 7 int main()
 8 {
 9     int n,i,counts;
10     scanf("%d",&n);
11     counts=0;
12     pr[0]=1;
13     pr[1]=1;///根节点的父亲节点是自身
14     for(i=2;i<=n;i++)
15     {
16         scanf("%d",&pr[i]);
17     }
18     for(i=1;i<=n;i++)
19     {
20         scanf("%d",&a[i]);
21     }
22     for(i=1;i<=n;i++)
23     {
24         if(a[i]!=a[pr[i]])///父亲节点和自身颜色不同
25         {
26             counts++;
27         }
28     }
29     printf("%d
",counts);
30     return 0;
31 }

 

 

以上是关于Coloring a Tree(耐心翻译+思维)的主要内容,如果未能解决你的问题,请参考以下文章

Coloring Game(思维题)

Codeforces 1296E2 String Coloring (hard version)(LIS,思维)

2020ICPC昆明 L.Simone and graph coloring(思维+nlog(n)的最长下降子序列)

すぬけ君の塗り絵 / Snuke's Coloring AtCoder - 2068 (思维,排序,贡献)

Educational Codeforces Round 49 E. Inverse Coloring(思维,差分,dp)

Codeforces1594 E2. Rubik‘s Cube Coloring (hard version)(思维+dp记忆化搜索)