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

来源:互联网 发布:服装成本统计软件 编辑:程序博客网 时间:2024/06/04 19:50

B. Coloring a Tree
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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

题意:给你一个n表示,一棵树有n个节点,我们可以对每个节点进行涂色,如果对某个节点涂色的话,则这个节点的所有孩子节点都将会变色,然后下一行表示第i个节点的前驱为ai,下一行表示为最后要把每个节点涂成对应的颜色即可。
题解:直接模拟即可

import java.io.BufferedInputStream;import java.io.PrintWriter;import java.util.*;public class Main {    public static void main(String []args){        Scanner in = new Scanner(new BufferedInputStream(System.in));        PrintWriter out = new PrintWriter(System.out);        Task task = new Task();        task.solve(in,out);        out.close();    }    static class Task{        public void solve(Scanner in , PrintWriter out) {            int n = in.nextInt();            Vec [] vec = new Vec[n + 1];            for(int i = 0 ; i <= n ;i++){                vec[i] = new Vec();            }            for(int i = 2 ; i <= n ; i++){                vec[in.nextInt()].list.add(i);            }            int [] color = new int[n + 1];            int [] colorp = new int[n + 1];            for(int i = 1 ; i <= n ; i++){                color[i] = in.nextInt();            }            int count = 0;            for(int i = 1 ; i <= n ; i++){                if(i == 1){                    colorp[i] = color[i];                    count++;                    for(int x : vec[i].list){                        colorp[x] = colorp[i];                    }                } else {                    if(colorp[i] != color[i]){                        count++;                        colorp[i] = color[i];                    }                    for(int x:vec[i].list){                        colorp[x] = colorp[i];                    }                }            }            out.println(count);        }    }    static class Vec{        List<Integer> list = new ArrayList<>();    }}