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<>(); }}
- Codeforces Round #453 (Div. 2) B. Coloring a Tree
- Codeforces Round #369 (Div. 2) 711C Coloring Tree (DP)
- Codeforces Round #453 (Div. 2) A,B
- codeforces 902 B. Coloring a Tree
- Codeforces Round #363 (Div. 1) B. Fix a Tree
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #185 (Div. 2)--A,B
- Codeforces Round #216 (Div. 2) A.B
- Codeforces round #236Div.2--A;B
- Codeforces Round #236 (Div. 2)------A,B
- Codeforces Round #237 (Div. 2) A & B
- Codeforces Round #247 (Div. 2) A B
- Codeforces Round #251 (Div. 2) A~B
- Codeforces Round #249 (Div. 2) A B
- Codeforces Round #271 (Div. 2)-A.B
- Codeforces Round #202 (Div. 2) A,B
- Codeforces Round #277 (Div. 2) A B
- Codeforces Round #278 (Div. 2) A B
- color.xml资源文件
- 谈一谈AI界的当红明星Python,是什么让Python这么火
- Translucent Bar 透明状态栏---沉浸式状态栏
- IntelliJ IDEA中MAVEN项目如何跳过测试?
- Win7系统下仅CPU模式的TensorFlow安装
- Codeforces Round #453 (Div. 2) B. Coloring a Tree
- 通过Intent启动其他APP的Activity
- centos 6.8 + pgsql 9.6 + adminpack
- Eclipse快速构造方法
- angularjs简单使用批量删除
- THINKPHP 防止重复提交表单 自己实现token
- WOJ 1016
- ubuntu进入root界面快捷键
- SimpleDateFormat 24小时和12小时转换