【Codeforces Round #453 (Div. 2) B】Coloring a Tree

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

从根节点开始。 显然它是什么颜色.就要改成对应的颜色。(如果上面已经有某个点传了值就不用改 然后往下传值。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 1e4;

int n,c[N+10],ans;
vector <int> g[N+10];

void dfs(int x,int C){
    int flag = C;
    if (C!=c[x]){
        ans++;
        flag = c[x];
    }
    for (int i = 0;i < (int) g[x].size();i++){
        int y = g[x][i];
        dfs(y,flag);
    }
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
    cin >>n;
    for (int i = 2;i <= n;i++){
        int fa;
        cin >> fa;
        g[fa].push_back(i);
    }
    for (int i = 1;i <= n;i++) cin >> c[i];
    dfs(1,0);
    cout << ans << endl;
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/8073110.html