Day2-O-Coloring a Tree CodeForces-902B
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
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
(图片好像挂了,具体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的更多相关文章
- codeforces902B. Coloring a Tree
B. Coloring a Tree 题目链接: https://codeforces.com/contest/902/problem/B 题意:给你一颗树,原先是没有颜色的,需要你给树填色成指定的样 ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- Distance in Tree CodeForces - 161D
Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- Codeforces 902B - Coloring a Tree
传送门:http://codeforces.com/contest/902/problem/B 本题是一个关于“树”的问题. 有一棵n个结点的有根树,结点按照1~n编号,根结点为1.cv为结点v的色号 ...
- 【Codeforces Round #453 (Div. 2) B】Coloring a Tree
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从根节点开始. 显然它是什么颜色.就要改成对应的颜色.(如果上面已经有某个点传了值就不用改 然后往下传值. [代码] #includ ...
- C - Ilya And The Tree Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...
- Coloring a Tree(耐心翻译+思维)
Description You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the ...
- AC日记——Propagating tree Codeforces 383c
C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- 【代码审计】VAuditDemo 文件包含漏洞
在 index.php中先判断get过来的module是否设置了变量,如果已经设置,则包含module,并与字符串.inc拼接 inc格式一般是图标或者头像格式,因此我们可以初步判断,这个包含应该是基 ...
- 解决前端项目启动时报错:Use // eslint-disable-next-line to ignore the next line.
首先说一下这个问题产生的原因: 项目创建时设置了使用 eslint 进行代码规范检查. 解决办法: 找到webpack.base.conf.js文件,并且将下满这行代码注释掉. ...(config. ...
- Catalyst3560密码破解
1.关机:2.将设备MODE键按住,然后开机,进入switch:switch: flash_initInitializing Flash...flashfs[0]: 7 files, 2 direct ...
- leetcode 0214
目录 ✅ 965. 单值二叉树 描述 解答 c++ updated dfs c++ py py 生成器 ✅ 762. 二进制表示中质数个计算置位 描述 解答 cpp other cpp mine ja ...
- python调用os模块锁定用户
import timeimport osuser_info = { 'mac': {'pwd': '123', 'count': 0, 'locked': False}, 'tank': {'pwd' ...
- __str__()方法和__repr__()方法
有时候我们想让屏幕打印的结果不是对象的内存地址,而是它的值或者其他可以自定义的东西,以便更直观地显示对象内容,可以通过在该对象的类中创建或修改__str__()或__repr__()方法来实现(显示对 ...
- 【PAT甲级】1058 A+B in Hogwarts (20 分)
题意: 输入两组,每组三个非负整数A,B,C(A<=1e7,B<17,C<29),输出相加的和.(类似个位上29进制,十位上17进制运算) AAAAAccepted code: #d ...
- Mysql基本用法-left join、right join、 inner join、子查询和join-02
left join #左连接又叫外连接 left join 返回左表中所有记录和右表中连接字段相等的记录 test_user表 phpcvs表 SQL: select * from test_use ...
- Python 爬取 热词并进行分类数据分析-[解释修复+热词引用]
日期:2020.02.02 博客期:141 星期日 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...
- 1-9springboot之thymeleaf常用语法(html页面)
一.引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 在html中引入此命名空间,可避免编辑器出现html验证错误,虽 ...