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. Linux命令备忘录:quota显示磁盘已使用的空间与限制

    quota命令用于显示用户或者工作组的磁盘配额信息.输出信息包括磁盘使用和配额限制. 语法 quota(选项)(参数) 选项 -g:列出群组的磁盘空间限制: -q:简明列表,只列出超过限制的部分: - ...

  2. hdu6370 并查集+dfs

    Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. POJ1426 Find The Multiple 解题报告

    参考:http://www.cnblogs.com/ACShiryu/archive/2011/07/24/2115356.html #include <iostream> #includ ...

  4. BZOJ3265: 志愿者招募加强版(线性规划)

    Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 809  Solved: 417[Submit][Status][Discuss] Descriptio ...

  5. python2.7入门---网络编程(socket)

        Python 提供了两个级别访问的网络服务: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的全部方法. 高级别 ...

  6. Oozie 配合 sqoop hive 实现数据分析输出到 mysql

    文件/RDBMS -> flume/sqoop -> HDFS -> Hive -> HDFS -> Sqoop -> RDBMS 其中,本文实现了 使用 sqoo ...

  7. 【转】iOS库 .a与.framework区别

    转自:http://blog.csdn.net/lvxiangan/article/details/43115131 一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 二.静态库与动态 ...

  8. CSS3实现加载数据动画2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Python的logging模块、os模块、commands模块与sys模块

    一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...

  10. 三张照片解决--win10系统的edge浏览器设置为浏览器IE8,IE7,IE9---完美解决 费元星

      主要思想:         第二种方法:     参考文档: 1.可以在系统盘的C:\Program Files\Internet Explorer中找到iexplore.exe,然后将其发送到桌 ...