codeforces902B. Coloring a Tree
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的更多相关文章
- Coloring a Tree(耐心翻译+思维)
Description You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the ...
- 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 ...
- 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 ...
- Codeforces Round #453 Div. 2 A B C D (暂时)
// 从大作业和实验报告中爬出来水一发 // 补题...还是得排在写完实验报告之后... A. Visiting a Friend 题意 给定若干段行车区间,问能否从起点到终点 思路 扫描一遍,维护最 ...
- Codeforces Round #453
Visiting a Friend Solution Coloring a Tree 自顶向下 Solution Hashing Trees 连续2层节点数都超过1时能异构 Solution GCD ...
- 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 ...
- 牛客网暑期ACM多校训练营(第三场)G:Coloring Tree(函数的思想)
之前两次遇到过函数的思想的题,所以这次很敏感就看出来了.可以参考之前的题: https://www.cnblogs.com/hua-dong/p/9291507.html Christmas is c ...
- 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 ...
随机推荐
- spring boot打包问题
java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories ...
- JAVA学习一 对象数组
对象数组 今天在写一个代码,才发现自己对于对象数组的理解是不够的,那么就讲讲自己现在的理解. 对于数组中的每一个元素都是一个针对对象的引用 他会指向你的具体的一个堆上的对象,它本身知识一个地址值,与其 ...
- 41-Individual authentication 模板
1-创建项目,进入vscode控制台,输出如下命令, uld表示指定mssqllocaldb E:\coding\netcore>dotnet new mvc -au Individual -u ...
- 关于xampp 集成开发包电脑重启mysql无法启动的问题
关于xampp 集成开发包电脑重启mysql无法启动的问题. 在做php开发时,安装过xampp,也不知道是版本老了还是什么问题,总是出现当天晚上下班关机,第二天上班mysql不能启动,在网上查找些资 ...
- ORB-SLAM 代码笔记(三)tracking原理
ORB视觉里程计主体在tracking线程中
- Mysql双主操作
MySQL双主(主主)架构方案 在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果 ...
- 使用PSSH批量操作Linux服务器
简介 服务器多了,有一个问题就是如何批量快速操作多台服务器,在网上搜到了PSSH工具,试用了一下发现挺好用,推荐给大家. pssh是一个python编写的可以在多台服务器上执行命令的轻量级管理工具,同 ...
- MySQL☞between ... and ...
between 初值 and 终值:求出该列列值在初值和终值之间所有的数据 格式如下: select 列名/* from 表名 where 列名 between 初值 and 终值 如下图:
- 接口自动化测试框架Karate入门
介绍 在这篇文章中,我们将介绍一下开源的Web-API自动化测试框架--Karate Karate是基于另一个BDD测试框架Cucumber来建立的,并且共用了一些相同的思想.其中之一就是使用Gher ...
- devstack环境搭建
1. devstack部署 参考Quick Start,推荐使用ubuntu16.04进行安装 1.1 配置ubuntu国内源 修改/etc/apt/sources.list内容为 deb http: ...