思路
无论其他节点如何染色,根节点一定会需要染一次色,之后会导致他的子树颜色变得和他一样,前面就白染色了,所以第一步一定是染根节点,之后对他的子节点染色就好,也就是说从根节点往下遍历,若子节点与父节点颜色不一致就需要染色一次,还是很好理解的。
代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> a(10007),b(10007);
for(int i=0; i<n-1; i++)
cin >> a[i];
for(int i=0; i<n; i++)
cin >> b[i];
set<pair<int,int>> st;
st.insert({0,0});
for(int i=0; i<n-1; i++)
{
st.insert({a[i],i+1});
}
int ans=1;
auto it = st.begin();
it++;
for(; it!=st.end(); it++)
{
if(b[it->second]!= b[it->first-1])
ans++;
}
cout << ans << endl;
return 0;
}