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


    Description

    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.

    Sample Input

    Input
    6
    1 2 2 1 5
    2 1 1 1 1 1
    Output
    3
    Input
    7
    1 1 2 3 1 4
    3 3 1 1 1 2 3
    Output
    5

    Hint

    The tree from the first sample is shown on the picture (numbers are vetices' indices):

    On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors):

    On seond step we color all vertices in the subtree of vertex 5 into color 1:

    On third step we color all vertices in the subtree of vertex 2 into color 1:

    The tree from the second sample is shown on the picture (numbers are vetices' indices):

    On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors):

    On second step we color all vertices in the subtree of vertex 3 into color 1:

    On third step we color all vertices in the subtree of vertex 6 into color 2:

    On fourth step we color all vertices in the subtree of vertex 4 into color 1:

    On fith step we color all vertices in the subtree of vertex 7 into color 3:

       题目意思: 给你n个点,代表这一棵树有n个节点。第二行内容是建树的关系,(开始我一直看不明白是如何建树的,后来翻译给队友,他帮我指了)

       从第二个节点开始的节点和父节点(上一个节点)相连,

    例如:1 2 2 1 5
    代表:节点2和节点1相连,节点3和节点2相连,节点4和节点2相连,节点5和节点1相连,节点6和节点5相连。

    第三行内容是需要将各个点涂成的颜色,给这个树涂色,有这么一条原则就是给某一节点涂色,以其为根节点的子树也将变为相应的颜色,我们可以成为一种颜料的溢出,问你最终需要

    最少需要涂多少次颜色就可以满足题目要求。

    解题思路:我们可以这样来思考,因为最后需要使所有的点都涂成要求的颜色,一定是按照从根节点到叶子节点遍历的涂色,但所有的点都遍历会造成浪费,我们只需要找出需要涂的点即可,
    那么哪些点需要涂呢?我们发现只有那些最后要求的其父亲节点和本身不同色的需要涂色,因为需要向下改变自身颜色,那么只需要统计这样点的个数即可。


     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int pr[10010];
     6 int a[10010];
     7 int main()
     8 {
     9     int n,i,counts;
    10     scanf("%d",&n);
    11     counts=0;
    12     pr[0]=1;
    13     pr[1]=1;///根节点的父亲节点是自身
    14     for(i=2;i<=n;i++)
    15     {
    16         scanf("%d",&pr[i]);
    17     }
    18     for(i=1;i<=n;i++)
    19     {
    20         scanf("%d",&a[i]);
    21     }
    22     for(i=1;i<=n;i++)
    23     {
    24         if(a[i]!=a[pr[i]])///父亲节点和自身颜色不同
    25         {
    26             counts++;
    27         }
    28     }
    29     printf("%d
    ",counts);
    30     return 0;
    31 }
    
    
    
     
  • 相关阅读:
    20190729
    [51Nod1623] 完美消除
    [WC2013] 糖果公园
    20190727
    在Java中调用带参数的存储过程
    Oracle 字符集的查看和修改
    查询oracle server端的字符集
    Mysql 数据库中文乱码问题
    错误:”未能加载文件或程序集“System.Web.Mvc, Version=2.0.0.0” 解决方法
    CSS背景图拉伸自适应尺寸,全浏览器兼容
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9495233.html
Copyright © 2020-2023  润新知