B. Coloring a Tree

题目链接:

https://codeforces.com/contest/902/problem/B

题意:给你一颗树,原先是没有颜色的,需要你给树填色成指定的样子,每次填色的话,子树会和根节点变成同一种颜色,问需要多少次填色

题解:前向星建树,从每个父节点便利,如果父节点的颜色和子节点不一样就ans++吗,最后记得把第一次操作(根节点涂色的次数 给记上

代码如下:

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 3e5+;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+;
int cnt;
int head[maxn],nxt[maxn],to[maxn];
int color[maxn];
int ans;
void add(int x,int y){
to[cnt]=y;
nxt[cnt]=head[x];
head[x]=cnt++;
} int main(){
#ifndef ONLINE_JUDGE
FIN
#endif
int n,x;
cnt=;
memset(head,-,sizeof(head));
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&x);
add(x,i);
}//前向星连边
for(int i=;i<=n;i++){
scanf("%d",&color[i]);
}
for(int i=;i<=n;i++){
for(int j=head[i];j!=-;j=nxt[j]){
//如果父节点和子树的节点不一样就操作一次
if(color[i]!=color[to[j]]) ans++;
}
}
cout<<ans+<<endl; //根节点的第一次操作算进去 }

codeforces902B. Coloring a Tree的更多相关文章

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

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

  2. Codeforces 902B - Coloring a Tree

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

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

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

  4. Codeforces Round #453 ( Div. 2) Editorial ABCD

    A. Visiting a Friend time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #453 Div. 2 A B C D (暂时)

    // 从大作业和实验报告中爬出来水一发 // 补题...还是得排在写完实验报告之后... A. Visiting a Friend 题意 给定若干段行车区间,问能否从起点到终点 思路 扫描一遍,维护最 ...

  6. Codeforces Round #453

    Visiting a Friend Solution Coloring a Tree 自顶向下 Solution Hashing Trees 连续2层节点数都超过1时能异构 Solution GCD ...

  7. Codeforces Round #369 (Div. 2) C 基本dp+暴力

    C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. 牛客网暑期ACM多校训练营(第三场)G:Coloring Tree(函数的思想)

    之前两次遇到过函数的思想的题,所以这次很敏感就看出来了.可以参考之前的题: https://www.cnblogs.com/hua-dong/p/9291507.html Christmas is c ...

  9. 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 ...

随机推荐

  1. ruby 数据类型Symbol

    一.符号创建 符号是Symbol类的实例,使用冒号加一个标识符即可创建符号 :a :"This is a symno" 二.符号字符串相互转换 p :symbol.to_s #=& ...

  2. Windows Server 2012下手动配置IIS的文件夹访问权限

    当新建一个website的时候,一般情况下IIS对相应的物理文件夹的访问权限是不够的. 针对匿名认证(anonymous authentication)需要: 打开文件夹properties-> ...

  3. python2.7入门---循环语句(while)

        接下来就要了解循环语句了.我们都知道,程序在一般情况下是按顺序执行的.编程语言提供了各种控制结构,允许更复杂的执行路径.循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环 ...

  4. nginx proxy_cache缓存详解

    目录 1. 关于缓冲区指令 1.1 proxy_buffer_size 1.2 proxy_buffering 1.3 proxy_buffers 1.4 proxy_busy_buffers_siz ...

  5. centos redis 安装 php-redis扩展安装 及使用

    前提:centos7.php7 安装redis-server 1:yum install redis 编译安装php-redis 扩展 1:下载编译安装 wget https://codeload.g ...

  6. 分享 go语言爬虫---开源项目Pholcus

    写在开头的话:记录一下最近学习Pholcus(https://github.com/henrylee2cn/pholcus)的过程,首先去学习的go基本语法,在没接触的时候发现很多不理解的地方,但是当 ...

  7. Linux通配符与特殊符号知识大全汇总

    符号 作用 Linux通配符 * 匹配任意(0个或多个)字符或字符串,包括空字符串 ? 匹配任意1个字符,有且只有一个字符 [abcd] 匹配abcd中任何一个字符,abcd也可是其他任意不连续字符 ...

  8. kubernetes(k8s) 集群

    开启和重启命令: sudo systemctl stop etcdsudo systemctl stop dockersudo systemctl stop kube-apiserversudo sy ...

  9. 基于Vue、web3的以太坊项目开发及交易内幕初探 错误解决总结

    基于Vue.web3的以太坊项目开发及交易内幕初探 本文通过宏观和微观两个层面窥探以太坊底层执行逻辑. 宏观层面描述创建并运行一个小型带钱包的发币APP的过程,微观层面是顺藤摸瓜从http api深入 ...

  10. lintcode-109-数字三角形

    109-数字三角形 给定一个数字三角形,找到从顶部到底部的最小路径和.每一步可以移动到下面一行的相邻数字上. 注意事项 如果你只用额外空间复杂度O(n)的条件下完成可以获得加分,其中n是数字三角形的总 ...