cf 902 B. Coloring a Tree

B. Coloring a Tree
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
Copy
6
1 2 2 1 5
2 1 1 1 1 1
output
Copy
3
input
Copy
7
1 1 2 3 1 4
3 3 1 1 1 2 3
output
Copy
5
Note

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:


树的构建和递归。

#include <bits/stdc++.h>

using namespace std;

int head[10005];
int cc[10005];
int ans;

struct node{
    int c;
    int next;
};
node tree[10005];
void add_edge(int from,int to){
    tree[to].next=head[from];
    head[from]=to;
}

void dfs(int x){
    if(head[x]==0)return;
    tree[x].c=cc[x];
    for(int i=head[x];i!=0;i=tree[i].next){
        if(cc[i]!=tree[x].c){
            ans++;
        }
        dfs(i);
    }
}

void solve(){
    int n;
    scanf("%d",&n);
    for(int i=2;i<=n;i++){
        int a;
        scanf("%d",&a);
        add_edge(a,i);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&cc[i]);
    }


    dfs(1);
    ans++;
    printf("%d",ans);


    return;
}

int main(){
    solve();
    return 0;
}

也可以不建树学习了,大神的思路就是不一样。

#include <bits/stdc++.h>

using namespace std;

int f[10005];
int c[10005];
void solve(){
    int n;
    scanf("%d",&n);
    for(int i=2;i<=n;i++){
        int a;
        scanf("%d",&a);
        f[i]=a;
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&c[i]);
    }
    int ans=1;
    for(int i=2;i<=n;i++){
        if(c[i]!=c[f[i]]){
            ans++;
        }
    }
    printf("%d",ans);


    return;
}

int main(){
    solve();
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值