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. latex02-LaTeX源文件的基本结构

    1.一个latex文件有且仅有一个document环境 %后表示注释 2.latex的导言区用于全局设置. 例如:title\author\date 加入空行是结构更加清晰 为了能正确的使用标题信息, ...

  2. 嵌入式linux系统移植(一)

    内容:   交叉编译环境   bootloader功能子系统   内核核心子系统   文件系统子系统要点:  搭建交叉编译环境  bootloader的选择和移植  kernel的配置.编译.移植和调 ...

  3. 《深入浅出MFC》– Document-View深入探讨

    1.其实Document/View不是什么新东西,Xerox PARC实验室是这种观念的滥觞.它是Smalltalk环境中的关键性部分,在那里它被称为Model-View-Controller(MVC ...

  4. SapScript

    * [OPEN_FORM] SAPscript: フォーム印刷の開始 * [START_FORM] SAPscript: 書式を開始 * [WRITE_FORM] SAPscript: 書式ウィンドウ ...

  5. SAPFiori

    最新SAP Fiori常用事务代码持续更新中...谢谢支持   注意: 以 / 开头的事务码需要加/N或/O进入,否则进不去   SEGW:  创建Gateway Service   /UI2/FLP ...

  6. kylin实战系列(一)

    kylin实战系列(一) 把之前kylin的实践小结一下,以备以后查看.

  7. performance_schema实现套路

      实现中大量使用多层次嵌套预编译,大量if else,wrapper等 不建议去看这个实现 start_mutex_wait_v1 if (flag_thread_instrumentation) ...

  8. 第一篇 Charles的配置及相关使用

    // Charles Proxy License // 适用于Charles任意版本的注册码,谁还会想要使用破解版呢. // Charles 4.2目前是最新版,可用.   Registered Na ...

  9. 3、shader深度测试(Cull、ZWrite 、ZTest )

    剔除和深度测试是渲染法线中的一个流程 Cull:默认情况下,Unity中给的所有Shader都是单面的,它都把反面的渲染给关闭掉了,如果你在开发的过程中需要使用到双面,只要把cull关闭(off)即可 ...

  10. golang模拟新浪微博登录

    1.基于幽灵蛛pholcus开源项目的规则 直接贴代码,代码可以更改后用于其他爬虫项目 package pholcus_lib // 基础包 import ( // "github.com/ ...