902B - Coloring a Tree

B. Coloring a Tree
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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
Note

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

902B - Coloring a Tree_第1张图片

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

902B - Coloring a Tree_第2张图片

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

902B - Coloring a Tree_第3张图片

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

902B - Coloring a Tree_第4张图片

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

902B - Coloring a Tree_第5张图片

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

902B - Coloring a Tree_第6张图片

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

902B - Coloring a Tree_第7张图片

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

902B - Coloring a Tree_第8张图片

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

902B - Coloring a Tree_第9张图片

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

902B - Coloring a Tree_第10张图片


题意:题意不是很容易读懂,要花一些时间读题.......输入一个n,代表一棵树有几个节点。然后第二行是从第二个节点开始的节点和父节点(上一个节点)相连,也就是样例1来说,相连有了路径就是那种树的形状,题目中这句话说了的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.。第三行代从第一个节点开始我们需要每个节点涂的颜色。初始化的颜色为0.如果父节点颜色改变了,所有子节点的颜色都要变成父节点的颜色。例如样例1对根节点涂色为2,所有节点都变为2这种颜色,2号节点变为1它的所有子节点(3,4节点)都变为1,接下来5号节点,改变为1,下面6好节点变成它的父节点(5)的颜色1.经过了三次改变就变成需要的涂的颜色。让我们输出最少的步骤把树涂成我们需要涂的颜色。

题解:最开始我看到题的时候,题都读的懵逼的,思路也没有。看了博客都是写的搜索,搜索树的层次然后颜色的改变,又不想写那么长,看了q神代码,还有点不能理解,就去问了学长,就明白了,用数组处理代码更简短,用到搜索的思想。开2个数组,a数组保存给的路径,也就是当前节点的父节点,b数组保存每个节点的颜色。在扫一遍,判断当前节点是不是和当前节点的父节点颜色相同,不同的话,就代表当前节点需要涂色,cnt++,简化了搜索过程中每一个父节点改变,子节点就要变的过程。

#include
using namespace std;
const int N=1e4+10;
int a[N],b[N];
int main()
{
    int n,cnt;
    while(cin>>n)
    {  cnt=0;
        for(int i=2; i<=n; i++)
            cin>>a[i];
        for(int i=1; i<=n; i++)
            cin>>b[i];
        for(int i=1; i<=n; i++)
            if(b[i]!=b[a[i]])
                cnt++;
        cout<

DFS 版:
#include 
#define pb push_back
using namespace std;
const ll MOD = 1e9+7;
int n,p[10005],c[10005],v[10005],curr[10005];
vector  x[10005];
int dfs(int node)
{
	int val=0;
	v[node]=1;
	if(curr[node]!=c[node])
	{
		curr[node]=c[node];
		val++;
	}
	for(int i=0;i

你可能感兴趣的:(ACM日常水题练习集)