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可以看一下原题)

思路:从树根1开始,层序遍历涂色,DFS+BFS,代码如下:

const int maxm = 1e4+;

int color[maxm], now[maxm], n, tmp, t;
vector<int> son[maxm]; void dfs(int i, int col) {
for(auto j : son[i])
dfs(j, col);
now[i] = col;
} int main() {
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &tmp);
son[tmp].push_back(i);
}
for (int i = ; i <= n; ++i)
scanf("%d", &color[i]);
queue<int> q;
q.push();
while(!q.empty()) {
tmp = q.front(), q.pop();
if(color[tmp] != now[tmp]) {
dfs(tmp, color[tmp]);
++t;
}
for(auto i : son[tmp])
q.push(i);
}
printf("%d\n", t);
return ;
}

DFS版层序,代码如下:

const int maxm = 1e4 + ;

int color[maxm], now[maxm], n, tmp, t;
vector<int> son[maxm]; void dfs1(int i, int col) {
for(auto j : son[i])
dfs1(j, col);
now[i] = col;
} void dfs2(int i) {
if(color[i] != now[i]) {
dfs1(i, color[i]);
++t;
}
for(auto j : son[i])
dfs2(j);
} int main() {
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &tmp);
son[tmp].push_back(i);
}
for (int i = ; i <= n; ++i)
scanf("%d", &color[i]);
dfs2();
printf("%d\n", t);
return ;
}

看到网上聚聚的解析,都不用建树搜索,直接判断某节点是否与其父节点目标颜色一致,若不一致一定需要额外一次染色,代码如下:

const int maxm = 1e4 + ;

int color[maxm], now[maxm], father[maxm], n, t;

int main() {
scanf("%d", &n);
for (int i = ; i <= n; ++i)
scanf("%d", &father[i]);
for (int i = ; i <= n; ++i)
scanf("%d", &color[i]);
for (int i = ; i <= n; ++i)
if(color[i] != color[father[i]])
++t;
printf("%d\n", t);
return ;
}

Day2-O-Coloring a Tree CodeForces-902B的更多相关文章

  1. codeforces902B. Coloring a Tree

    B. Coloring a Tree 题目链接: https://codeforces.com/contest/902/problem/B 题意:给你一颗树,原先是没有颜色的,需要你给树填色成指定的样 ...

  2. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  3. Distance in Tree CodeForces - 161D

    Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...

  4. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  5. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  6. Codeforces 902B - Coloring a Tree

    传送门:http://codeforces.com/contest/902/problem/B 本题是一个关于“树”的问题. 有一棵n个结点的有根树,结点按照1~n编号,根结点为1.cv为结点v的色号 ...

  7. 【Codeforces Round #453 (Div. 2) B】Coloring a Tree

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从根节点开始. 显然它是什么颜色.就要改成对应的颜色.(如果上面已经有某个点传了值就不用改 然后往下传值. [代码] #includ ...

  8. C - Ilya And The Tree Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...

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

    Description You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the ...

  10. AC日记——Propagating tree Codeforces 383c

    C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. 用for循环写这段代码

    之前用while循环写了一段代码,现在改为用for循环来写,代码如下: hongtao_age = 38 for i in range(5): guess_age = int(input(" ...

  2. ROM, RAM, NVRAM and Flash Memory on Cisco Routers

    当谈到路由器有多少内存以及哪些内存做什么时,有时人们会感到困惑. 您应该熟悉4个内存术语,在升级路由器的IOS之前应检查其中2个. 这些是以下内容: ROM:ROM代表只读存储器. 它存储System ...

  3. MAC系统 - 基础知识

    一.基础操作 设置:触控板设置 - >学习具体手势 手势:MacBook Pro手势大全必学手势触控板手势有哪些 左键,右键,滑屏,切换到应用... 一指操作: 一指敲击:鼠标左键: 一指按下: ...

  4. HDU1495 非常可乐(BFS/数论)

    大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多.但see ...

  5. VUE父子组件相互传值

    passer.vue中代码 首先在文件中引入组件 import canvasDraw from '@/components/CanvasDraw/canvasDraw' 局部注册组件:componen ...

  6. 七、SXSSFWorkbook生成大excle,避免内存溢出

    1.SXSSFWorkbook理解: SXSSFWorkbook是用来生成海量excel数据文件,主要原理是借助临时存储空间生成excel,SXSSFWorkbook专门处理大数据,对于大型excel ...

  7. liux vim 命令

    清除所有行 先 gg 再 dG

  8. Steam 游戏 《The Vagrant(流浪者)》修改器制作-[先使用CE写,之后有时间的话改用CheatMaker](2020年寒假小目标08)

    日期:2020.02.07 博客期:146 星期五 [温馨提示]: 只是想要修改器的网友,可以直接点击此链接下载: 只是想拿CT文件的网友,可以直接点击此链接下载: 没有博客园账号的网友,可以将页面下 ...

  9. SpringBoot与Mybatis-plus整合,代码生成mvc层

    一.添加pom依赖 <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifac ...

  10. bootstrap帮助文档

    概览 深入了解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML5 文档类型 Bootstrap 使用到的某些 HTML 元素和 CSS 属 ...