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
6
1 2 2 1 5
2 1 1 1 1 1
3
7
1 1 2 3 1 4
3 3 1 1 1 2 3
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:
从第二个节点开始的节点和父节点(上一个节点)相连,
例如:1 2 2 1 5
代表:节点2和节点1相连,节点3和节点2相连,节点4和节点2相连,节点5和节点1相连,节点6和节点5相连。 第三行内容是需要将各个点涂成的颜色,给这个树涂色,有这么一条原则就是给某一节点涂色,以其为根节点的子树也将变为相应的颜色,我们可以成为一种颜料的溢出,问你最终需要
最少需要涂多少次颜色就可以满足题目要求。 解题思路:我们可以这样来思考,因为最后需要使所有的点都涂成要求的颜色,一定是按照从根节点到叶子节点遍历的涂色,但所有的点都遍历会造成浪费,我们只需要找出需要涂的点即可,
那么哪些点需要涂呢?我们发现只有那些最后要求的其父亲节点和本身不同色的需要涂色,因为需要向下改变自身颜色,那么只需要统计这样点的个数即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int pr[10010];
int a[10010];
int main()
{
int n,i,counts;
scanf("%d",&n);
counts=0;
pr[0]=1;
pr[1]=1;///根节点的父亲节点是自身
for(i=2;i<=n;i++)
{
scanf("%d",&pr[i]);
}
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
if(a[i]!=a[pr[i]])///父亲节点和自身颜色不同
{
counts++;
}
}
printf("%d\n",counts);
return 0;
}
Coloring a Tree(耐心翻译+思维)的更多相关文章
- codeforces902B. Coloring a Tree
B. Coloring a Tree 题目链接: https://codeforces.com/contest/902/problem/B 题意:给你一颗树,原先是没有颜色的,需要你给树填色成指定的样 ...
- 【2019.10.7 CCF-CSP-2019模拟赛 T1】树上查询(tree)(思维)
思维 这道题应该算是一道思维题吧. 首先你要想到,既然这是一棵无根树,就要明智地选择根--以第一个黑点为根(不要像我一样习惯性以\(1\)号点为根,结果直到心态爆炸都没做出来). 想到这一点,这题就很 ...
- Binary Tree(二叉树+思维)
Binary Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)
题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...
- CodeForces - 963B Destruction of a Tree (dfs+思维题)
B. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)
Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do s ...
- Masha and Bears(翻译+思维)
Description A family consisting of father bear, mother bear and son bear owns three cars. Father bea ...
- CF482D Random Function and Tree 树形DP + 思维 + 神题
Code: #include<bits/stdc++.h> #define ull unsigned long long #define MOD 1000000007 #define ll ...
- Codeforces 902B - Coloring a Tree
传送门:http://codeforces.com/contest/902/problem/B 本题是一个关于“树”的问题. 有一棵n个结点的有根树,结点按照1~n编号,根结点为1.cv为结点v的色号 ...
随机推荐
- C++_类和对象
类和对象 OOP第二课 1 类的构成 1.1 从结构到类 1.2 类的构成 2 成员函数的声明 2.1 普通成员函数形式 2.2 将成员函数以内联函数的形式进行说明 3 对象的定义和使用 3.1 对象 ...
- solve the promble of VMware Workstation Ubuntu18.04 ethernet interface losting
$ ifconfig -aens33: flags=4098<BROADCAST,MULTICAST> mtu 1500 ether 00:**:**:**:**:** txqu ...
- My SQL常用操作汇总
写这篇随笔的目的是我发现了在上一篇关于My SQL的随笔中存在一些不严谨的代码问题,在这里再次简单的总结一下并加以改进,以代码为主. # !每行命令必须以分号(;)结尾 先通过命令行进入数据库客户端 ...
- 菜鸟级渣渣 关于MAC系统开发java的吐槽
最开始买电脑的时候不知道为什么脑子一抽买了个苹果.因为不知道和谁聊的.后期服务器大部分都是linux系统,后期也要学linux系统.mac系统类似linux系统.然后就买了个mac,感觉凭借自己的聪明 ...
- MyEclipse报错:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException Communications link failure
数据库服务没有开或者是驱动那块的问题
- CDH升级 5.7.5 --> 5.13.3(tar包方式)
博客园首发,转载请注明出处:https://www.cnblogs.com/tzxxh/p/9123231.html 一.准备 1.关闭cdh中的服务 hdfs.yarn等所有服务:关闭 cm-ser ...
- x01.os.24: 来点代码
<Orange'S 一个操作系统的实现>源代码 <Linux 0.11 内核完全注释>源代码 linux-0.12 源代码: 解决了 Not Owner 问题 闲来无事,在 ...
- Java动态代理代码快速上手
动态代理的两个核心的点是:代理的行为 和 代理机构. 举个例子,上大学的时候,很多同学吃午饭的时候都是叫别人带饭,有一个人H特别热心肠,想了一个办法,他在门口挂了个公示牌,每天有谁想要找人带饭就写公告 ...
- golang 项目实战简明指南
原文地址 开发环境搭建 golang 的开发环境搭建比较简单,由于是编译型语言,写好 golang 源码后,只需要执行 go build 就能将源码编译成对应平台(本文中默认为 linux)上的可执行 ...
- em,rem区别比较
rem是基于html元素的字体大小来决定,而em则根据使用它的元素的大小决定. 注意:很多人错误以为em是根据父类元素,实际上是使用它的元素继承了父类元素的属性才会产生的错觉. 主要区别 em 和 r ...